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>
 
https://www.cnblogs.com/gaobw/p/6553443.html

WPF 附加属性的用法 (一)的更多相关文章

  1. wpf附加属性理解

    WPF附加属性 http://www.cnblogs.com/tianyou/archive/2012/12/27/2835670.html WPF属性(二)附加属性 http://blog.csdn ...

  2. WPF Visibility属性用法

    WPF Visibility属性用法 Visible 元素在窗体中正常显示 Collaspsed 元素不显示,也不占用空间 Hidden 元素不显示,但是任然为它保留空间

  3. WPF的依赖属性和附加属性(用法解释较全)

    转:https://www.cnblogs.com/zhili/p/WPFDependencyProperty.html 一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己 ...

  4. WPF的DataGrid用法-小白向

    前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功能实现所折磨.网络上的解决方法太多,但也太杂.没法子,我只好硬着头皮阅览各种文献资料,然后不断的去尝试,总算小有成果 ...

  5. wpf附加属性详解

    为什么使用附加属性 附加属性的一个用途是允许不同的子元素为父元素中定义的属性指定唯一的值. 此方案的一个具体应用是,让子元素通知父元素它们在用户界面 (UI) 中的呈现方式. 一个示例是 DockPa ...

  6. WPF附加属性

    附加属性实质也是依赖属性,是说一个属性本来不属于某个对象,但由于某种需求被后来附加上的,也就是说把对象放入一个特定环境后才具有的属性 例子:人在学校有年纪和班级两个属性,人放在学校里会获得年级和班级两 ...

  7. WPF中ComboBox用法

    The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, becaus ...

  8. WPF常用TriggerAction用法 (一)

    Microsoft.Expression.Interactivity 常用TriggerAction-> CallMethodAction ChangePropertyAction Contro ...

  9. WPF DevExpress ChartControl用法

    WPF常用的第三方控件集,DevExpress中ChartControl的使用 下面介绍如何生成Chart界面: <dxc:ChartControl AnimationMode="On ...

随机推荐

  1. ant Select 联动

    1.代码 /** * 选择监区 组件 */ import React, { PureComponent } from 'react'; import PropTypes from 'prop-type ...

  2. jumpserver v0.5.0 创建用户和管理机器

    用户管理-创建用户    data 用户详情 如下 创建用户组 data 资产列表添加资产 jumpserver 的 root 公钥需保持到 后端服务器的 authorized_keys 里, 然后测 ...

  3. Python-常用库扩展

    图片处理: PIL HTTP请求模拟: requests

  4. Linux Load average负载详细解释

    http://tianmaotalk.iteye.com/blog/1027970     Linux Load average负载详细解释   linux查看机器负载

  5. geopandas python地图绘制

    #geopandas python地理数据处理 瓦片地图:瓦片地图金字塔模型是一种多分辨率层次模型,从瓦片金字塔的底层到顶层,分辨率越来越低,但表示的地理范围不变.首先确定地图服务平台所要提供的缩放级 ...

  6. android之内容提供者解析

    该系统有两个应用,比较繁琐.但是内容提供者是android里非常非常重要的一个内容,我们得好好学习下哦.先看效果图,我们提供了四个按钮,点击按钮便会进行相应的操作. 我们先看内容提供者所在的应用,代码 ...

  7. Java 继承、多态与类的复用

    摘要: 本文结合Java的类的复用对面向对象两大特征继承和多态进行了全面的介绍. 首先,我们介绍了继承的实质和意义,并探讨了继承,组合和代理在类的复用方面的异同.紧接着,我们依据继承引入了多态.介绍了 ...

  8. ATITIT.翻译模块的设计与实现 api attilax 总结

    ATITIT.翻译模块的设计与实现 api attilax 总结 1. 翻译原理1 2. TMX格式是国际通用格式(xml)1 2.1. 方法/步骤2 3. TRADOS2 4. ATITIT.翻译软 ...

  9. [docker]docker日志驱动记录nginx日志情形探究

    这里研究下容器nginx记录日志的集中情况,主要想弄明白,docker的日志--log-driver=fluentd 改成fluentd后,会不会在本地在记录一份日志 整体架构是这样的 情况1: 首先 ...

  10. PHPCMS模块开发相关文件

    名称 类型说明 Guestbook 模块后台控制器 index 模块前台控制器 Install 安装文件夹 ├templates ├config.inc.php ├extention.inc.php ...