WPF 附加属性的用法 (一)
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>

WPF 附加属性的用法 (一)的更多相关文章
- wpf附加属性理解
WPF附加属性 http://www.cnblogs.com/tianyou/archive/2012/12/27/2835670.html WPF属性(二)附加属性 http://blog.csdn ...
- WPF Visibility属性用法
WPF Visibility属性用法 Visible 元素在窗体中正常显示 Collaspsed 元素不显示,也不占用空间 Hidden 元素不显示,但是任然为它保留空间
- WPF的依赖属性和附加属性(用法解释较全)
转:https://www.cnblogs.com/zhili/p/WPFDependencyProperty.html 一.引言 感觉最近都颓废了,好久没有学习写博文了,出于负罪感,今天强烈逼迫自己 ...
- WPF的DataGrid用法-小白向
前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功能实现所折磨.网络上的解决方法太多,但也太杂.没法子,我只好硬着头皮阅览各种文献资料,然后不断的去尝试,总算小有成果 ...
- wpf附加属性详解
为什么使用附加属性 附加属性的一个用途是允许不同的子元素为父元素中定义的属性指定唯一的值. 此方案的一个具体应用是,让子元素通知父元素它们在用户界面 (UI) 中的呈现方式. 一个示例是 DockPa ...
- WPF附加属性
附加属性实质也是依赖属性,是说一个属性本来不属于某个对象,但由于某种需求被后来附加上的,也就是说把对象放入一个特定环境后才具有的属性 例子:人在学校有年纪和班级两个属性,人放在学校里会获得年级和班级两 ...
- WPF中ComboBox用法
The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, becaus ...
- WPF常用TriggerAction用法 (一)
Microsoft.Expression.Interactivity 常用TriggerAction-> CallMethodAction ChangePropertyAction Contro ...
- WPF DevExpress ChartControl用法
WPF常用的第三方控件集,DevExpress中ChartControl的使用 下面介绍如何生成Chart界面: <dxc:ChartControl AnimationMode="On ...
随机推荐
- Unity3D实现3D立体游戏原理及过程,需偏振眼镜3D显
http://tieba.baidu.com/p/3038509618?fr=ala0&pstaala=3
- 算法笔记_073:哈密顿回路问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 什么是哈密顿回路? 引用自百度百科: 哈密顿图(哈密尔顿图)(英语:Hamiltonian path,或Traceable path)是一个无向图, ...
- LoadRunner+Android模所器实现抓包并调试本地服务端
步骤就是 1:新建LR脚本.协议选择Mobile Application - HTTP/HTML 2:在record里选择第三个:Record Emulator........ 3: 选择下一步后, ...
- DevOps,不是一个传说!
转自: http://www.infoq.com/cn/articles/devops-not-legend DevOps最近成了热词,望文生义,你也能猜个八九不离十,它就是在说"研发团队& ...
- 使用Python爬虫爬取网络美女图片
代码地址如下:http://www.demodashi.com/demo/13500.html 准备工作 安装python3.6 略 安装requests库(用于请求静态页面) pip install ...
- Git-Git Book阅读笔记
git diff [fname] 查看工作区与缓存区异同 git diff --staged [fname] 查看缓存区与上次提交之间的差异 git diff HEAD [fname] 查 ...
- 使用QQ互联登录应用
QQ登录集成插件简介 互联网应用越来越多,通常每一个应用都会要求用户注册登录,粗略估记一下,QQ,微博,微信,银行帐号.邮箱,招聘网站账户,淘宝帐号,支付宝帐号,公司OA帐号....粗略算一下,十几个 ...
- sass 的学习
导入scss @import "../../sass/variables.scss"; @import "../../sass/helper.scss"; @m ...
- [Jobdu] 题目1337:寻找最长合法括号序列
题目描述: 给你一个长度为N的,由’(‘和’)’组成的括号序列,你能找出这个序列中最长的合法括号子序列么?合法括号序列的含义便是,在这个序列中,所有的左括号都有唯一的右括号匹配:所有的右括号都有唯一的 ...
- [docker]bind9.11-with-mysql5.6 docker容器化实战
参考: https://www.centos.bz/2012/09/bind-with-mysql-support/ http://blog.51niux.com/?id=125 http://470 ...