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 ...
随机推荐
- mysql中select distinct的使用方法
在使用mysql时,有时须要查询出某个字段不反复的记录,尽管mysql提供有distinct这个keyword来过滤掉多余的反复记录仅仅保留一条,但往往仅仅用它来返回不反复记录的条数,而不是用它来返回 ...
- PHP-XML基于流的解析器及其他常用解析器
PHP中有两种主要的XML解析器 1)基于树的解析器.它是把整个文档存储为树的数据结构中,即需要把整个文档都加载到内存中才能工作.所以,当处理大型XML文档时候,性能剧减.SimpleXML和DOM扩 ...
- CSS3Transition添加多个过渡效果
本篇文章由:http://xinpure.com/css3transition-to-add-multiple-transition-effects/ 通过监听动画的结束事件,可以为一个元素添加多个动 ...
- ubuntu下运行第一个.net core web程序
前置条件 ubuntu系统 且已经安装dotnetcore运行环境 mkdir testMVC 创建一个文件夹 cd testMVC 进入文件夹 dotnet new -t web 创建程序( ...
- linux 环境下git的安装与配置
git是一个分布式的版本控制工具 1.安装git相关的依赖 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-de ...
- mysql主从复制之mysql-proxy实现读写分离
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lansgg.blog.51cto.com/5675165/1242674 本文系 ...
- c++ 头文件循环引用解法
A.h #include "B.h" class A{ public: B* m_b; } B.h #include "A.h" class B{ public ...
- Codeforces Round #265 (Div. 2) B. Inbox (100500)
Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others ...
- matplotlib之legend
在<matplotlib极坐标系应用之雷达图> 中,我们提出了这个问题“图例中每种成员的颜色是怎样和极坐标相应的成员的颜色相对应的呢”,那么接下来我们说说legend的一般使用和设置. 调 ...
- PLINK pca
#coding:utf-8 __author__ = 'similarface' import os import pandas as pd import matplotlib.pyplot as p ...