目的:应用附加事件的方式定义可以绑定的事件,如MouseLeftButton、MouseDouble等等
一、定义属于Control的附加事件ControlAttachEvent类
-
/// <summary> 附加事件 </summary>
-
public static class ControlAttachEvent
-
{
-
-
#region - 双击事件 -
-
-
public static readonly DependencyProperty PreviewMouseDoubleClickProperty =
-
DependencyProperty.RegisterAttached("PreviewMouseDoubleClick", typeof(ICommand), typeof(ControlAttachEvent), new FrameworkPropertyMetadata(OnCommandChanged));
-
-
public static ICommand GetPreviewMouseDoubleClick(Control target)
-
{
-
return (ICommand)target.GetValue(PreviewMouseDoubleClickProperty);
-
}
-
-
public static void SetPreviewMouseDoubleClick(Control target, ICommand value)
-
{
-
target.SetValue(PreviewMouseDoubleClickProperty, value);
-
}
-
-
private static void Element_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
-
{
-
Control control = sender as Control;
-
-
ICommand command = GetPreviewMouseDoubleClick(control);
-
-
if (command.CanExecute(sender))
-
{
-
command.Execute(sender);
-
e.Handled = true;
-
}
-
}
-
-
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
-
{
-
Control control = d as Control;
-
-
control.PreviewMouseDoubleClick += new MouseButtonEventHandler(Element_PreviewMouseDoubleClick);
-
}
-
#endregion
-
-
public static DependencyProperty PreviewMouseLeftButtonDownCommandProperty = DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown",typeof(ICommand),typeof(ControlAttachEvent),new FrameworkPropertyMetadata(null, new PropertyChangedCallback(PreviewMouseLeftButtonDownChanged)));
-
-
public static void SetPreviewMouseLeftButtonDown(DependencyObject target, ICommand value)
-
{
-
target.SetValue(PreviewMouseLeftButtonDownCommandProperty, value);
-
}
-
-
public static ICommand GetPreviewMouseLeftButtonDown(DependencyObject target)
-
{
-
return (ICommand)target.GetValue(PreviewMouseLeftButtonDownCommandProperty);
-
}
-
-
private static void PreviewMouseLeftButtonDownChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
-
{
-
FrameworkElement element = target as FrameworkElement;
-
if (element != null)
-
{
-
if ((e.NewValue != null) && (e.OldValue == null))
-
{
-
element.PreviewMouseLeftButtonDown += element_PreviewMouseLeftButtonDown;
-
}
-
else if ((e.NewValue == null) && (e.OldValue != null))
-
{
-
element.PreviewMouseLeftButtonDown -= element_PreviewMouseLeftButtonDown;
-
}
-
}
-
}
-
-
private static void element_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
-
{
-
FrameworkElement element = (FrameworkElement)sender;
-
ICommand command = (ICommand)element.GetValue(PreviewMouseLeftButtonDownCommandProperty);
-
command.Execute(sender);
-
}
-
}
说明:当控件MyControl中应用该项附加事件,如注册PreviewMouseDoubleClick事件,则会触发更新方法OnCommandChanged,而在更新方法中则会注册该控件MyControl的双击事件绑定的命令,由此实现双击该控件触发绑定命令的功能;
二、调用方法
<Button base:ControlAttachEvent.PreviewMouseDoubleClick="{Binding RelayCommand}"/>
如上代码当双击Button时会触发ViewModel中绑定的RelayCommand命令
注:
优点在于可以把控件中不支持绑定的事件支持绑定,同时应用附加属性复用性更强
缺点在于传递事件的参数只有sender,agrs需要特殊处理,同时支持的事件与Control有关,其他更上层的控件需要单独定义