最近在使用SL的时候发现里面没有支持双击事件,虽然可以写代码实现,但是每次都要写一段处理代码很麻烦,于是把它封装成了一个类方便调用,粘上来共享一下
- // *** 实现 ***
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Collections.Generic;
- namespace DoubleClickTest
- {
- public class DoubleClick
- {
- int Interval;
- Dictionary<UIElement, TimeSpan> ControlMgr = new Dictionary<UIElement, TimeSpan>();
- /// <summary>
- /// DoubleClickEvent
- /// </summary>
- public event EventHandler<MouseButtonEventArgs> DoubleClickEvent = null;
- /// <summary>
- /// Construct
- /// </summary>
- /// <param name="interval"></param>
- public DoubleClick(int interval = 200)
- {
- this.Interval = interval;
- }
- /// <summary>
- /// Attach
- /// </summary>
- /// <param name="control"></param>
- public void Attach(UIElement ctrl)
- {
- if (!IsExist(ctrl))
- {
- this.ControlMgr[ctrl] = new TimeSpan(0);
- ctrl.MouseLeftButtonDown += new MouseButtonEventHandler(control_MouseLeftButtonDown);
- }
- }
- /// <summary>
- /// Deatch
- /// </summary>
- /// <param name="control"></param>
- public void Deatch(UIElement ctrl)
- {
- if (IsExist(ctrl))
- {
- ctrl.MouseLeftButtonDown -= new MouseButtonEventHandler(control_MouseLeftButtonDown);
- this.ControlMgr.Remove(ctrl);
- }
- }
- void control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
- {
- UIElement ctrl = sender as UIElement;
- if (ctrl != null)
- {
- TimeSpan now = new TimeSpan(DateTime.Now.Ticks);
- if ((now - ControlMgr[ctrl]).TotalMilliseconds <= Interval)
- {
- if (DoubleClickEvent != null)
- {
- DoubleClickEvent(sender, e);
- }
- }
- ControlMgr[ctrl] = now;
- }
- }
- bool IsExist(UIElement control)
- {
- TimeSpan result;
- return ControlMgr.TryGetValue(control, out result);
- }
- }
- }
演示
- <UserControl x:Class="DoubleClickTest.MainPage"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="400">
- <Grid x:Name="LayoutRoot" Background="White">
- <Ellipse Height="68" HorizontalAlignment="Left" Margin="35,23,0,0" Name="ellipse1" Stroke="Black" StrokeThickness="20" VerticalAlignment="Top" Width="102" />
- <TextBlock Height="46" HorizontalAlignment="Left" Margin="35,219,0,0" Name="textBlock1" Text="TextBlock" FontSize="22" VerticalAlignment="Top" Width="141" />
- <Rectangle Height="64" HorizontalAlignment="Left" Margin="35,125,0,0" Name="rectangle1" Stroke="Black" StrokeThickness="20" VerticalAlignment="Top" Width="122" />
- </Grid>
- </UserControl>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- namespace DoubleClickTest
- {
- public partial class MainPage : UserControl
- {
- /// <summary>
- /// DoubleClick object
- /// </summary>
- DoubleClick DoubleClickHandle = new DoubleClick();
- public MainPage()
- {
- InitializeComponent();
- // 注册控件的双击事件
- DoubleClickHandle.Attach(textBlock1);
- DoubleClickHandle.Attach(ellipse1);
- DoubleClickHandle.Attach(rectangle1);
- // 双击事件
- DoubleClickHandle.DoubleClickEvent += (s, e) =>
- {
- MessageBox.Show(s.GetType().FullName);
- };
- }
- }
- }