public class MDCTest { public static DependencyProperty MouseDoubleClickCommandProperty = DependencyProperty.RegisterAttached( "MouseDoubleClick", typeof(ICommand), typeof(MDCTest), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(MouseDoubleClickChanged)) ); public static void SetMouseDoubleClick(DependencyObject target, ICommand value) { target.SetValue(MDCTest.MouseDoubleClickCommandProperty, value); } public static ICommand GetMouseDoubleClick(DependencyObject target) { return (ICommand)target.GetValue(MDCTest.MouseDoubleClickCommandProperty); } private static void MouseDoubleClickChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { Control control = target as Control; if (control != null) { if (e.NewValue != null && e.OldValue == null) { control.MouseDoubleClick += new MouseButtonEventHandler(control_MouseDoubleClick); } else if (e.NewValue == null && e.OldValue != null) { control.MouseDoubleClick -= new MouseButtonEventHandler(control_MouseDoubleClick); } } } public static void control_MouseDoubleClick(object sender, MouseButtonEventArgs e) { Control control = sender as Control; ICommand command = (ICommand)control.GetValue(MDCTest.MouseDoubleClickCommandProperty); command.Execute(control); } }
public class RelayCommand : ICommand { private Action
public class LabelViewModel { public ICommand Command { get { return new RelayCommand((m) => MessageBox.Show(m.ToString() + "command双击事件成功")); } } }
给Label附加双击事件 原文:http://www.cnblogs.com/ptfblog/archive/2011/07/11/2103183.html
绑定有两个需要注意的地方
1.如果绑定到 附加属性(Binding Attached Property),需要加上括号,这个比较特别,例如
2.如果绑定到只读的属性(Binding to readonly property),例如IsFocused,需要加上 Mode = OneWay