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<object> _Execute; private Predicate<object> _CanExecute; public RelayCommand(Action<object> execte) : this(execte, null) { } public RelayCommand(Action<object> execute, Predicate<object> canExecute) { if (execute == null) throw new ArgumentNullException("Execute"); _Execute = execute; _CanExecute = canExecute; } public bool CanExecute(object parameter) { return _CanExecute == null ? true : _CanExecute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _Execute(parameter); } }
public class LabelViewModel { public ICommand Command { get { return new RelayCommand((m) => MessageBox.Show(m.ToString() + "command双击事件成功")); } } }
<Label Name="label" local:MDCTest.MouseDoubleClick="{Binding Path=Command}">MouseDoubleClickTest</Label>
给Label附加双击事件 原文:http://www.cnblogs.com/ptfblog/archive/2011/07/11/2103183.html
绑定有两个需要注意的地方
1.如果绑定到 附加属性(Binding Attached Property),需要加上括号,这个比较特别,例如
<TextBox x:Name="tbUserName" Width="200" Grid.Column="0" Margin="10,10,0,0" Foreground="Gray" nasSetting:TextBoxMaskHelper.MaskText="Please input username" Text="{Binding Path=(nasSetting:TextBoxMaskHelper.MaskText),Mode=OneWay,ElementName=tbUserName}" Height="30" CharacterCasing="Normal">
<TextBox.Foreground> <MultiBinding Converter="{StaticResource MaskBrushConverter}"> <Binding Path="Text" ElementName="tbUserName" UpdateSourceTrigger="PropertyChanged"/> <Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)" ElementName="tbUserName" /> </MultiBinding> </TextBox.Foreground>
2.如果绑定到只读的属性(Binding to readonly property),例如IsFocused,需要加上 Mode = OneWay
<TextBox.Text> <MultiBinding Converter="{StaticResource MaskTextConverter}" Mode="OneWay"> <Binding Path="(nasSetting:TextBoxMaskHelper.MaskText)" ElementName="tbUserName" /> <Binding Path="IsFocused" ElementName="tbUserName" Mode="OneWay"/> </MultiBinding> </TextBox.Text>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2017-02-21 ASP.NET动态添加控件一例
2017-02-21 Asp.net控制Tomcat启动关闭的实现方法
2017-02-21 正则表达式速查表(ASP.NET)
2017-02-21 c# 可变数目参数params实例