[WPF系列]基础 Listening to Dependency Property change notifications of a given Element
I want to share this great post from Anoop that shows a easy way to add a notification system to dependency properties of a given element. It creates and attaches a new property to the existing property and let’s you specify the PropertyChangedCallback eventhandler.
There are different examples on the internet, but i like how Anoop created a generic method with access to the callback event handler.
Here is the main part of the code:
/// Listen for change of the dependency property
public void RegisterForNotification(string propertyName, FrameworkElement element, PropertyChangedCallback callback)
{
//Bind to a depedency property
Binding b = new Binding(propertyName) { Source = element };
var prop = System.Windows.DependencyProperty.RegisterAttached(
"ListenAttached"+propertyName,
typeof(object),
typeof(UserControl),
new System.Windows.PropertyMetadata(callback)); element.SetBinding(prop, b);
}
And here is an example on how to use it:
//Shows a message box when the text of the Textbox changes.
RegisterForNotification
("Text", this.txtMain,(d,e)=>MessageBox.Show("Text changed"));
Read Anoop’s complete article for more information!
update (07/28/2011):
here is small update of the code so it also works with other objects instead of just FrameworkElements
private void RegisterForNotification(string propertyName, object source, PropertyChangedCallback callback)
{
Binding b = new Binding(propertyName);
b.Source = source; DependencyProperty prop = System.Windows.DependencyProperty.RegisterAttached(
"ListenAttached" + propertyName,
typeof(object),
this.GetType(),
new System.Windows.PropertyMetadata(callback)); BindingOperations.SetBinding(this, prop, b);
}
引用
Tips & Tricks: Listening to Dependency Property change notifications of a given Element
[WPF系列]基础 Listening to Dependency Property change notifications of a given Element的更多相关文章
- [WPF系列]-基础系列 Property Trigger, DataTrigger & EventTrigger
So far, we worked with styles by setting a static value for a specific property. However, using trig ...
- [WPF系列]-基础系列 TabControl应用
引言 Tabcontrol控件也是我们在项目中经常用到的一个控件,用它将相关的信息组织在一起分类显示. 简介 ========================================= ...
- [WPF系列]基础学习(一) WPF是什么?
引言 学习之前,我们首先大概了解下WPF诞生的背景以及它所能解决的问题或者新颖之处.WPF作为微软新一代的用户界面技术, WPF简介 WPF的全称是WindowsPresentationFound ...
- [WPF系列]基础Combox
示例 参考 WPF combobox SelectedValue binding to string Confused with wpf ComboBox DisplayMemberPath, ...
- [WPF系列]-使用Binding来同步不同控件的Dependency property
简介 项目中经常会用到,同步两个控件的值,本文就简单列举两种方式来同步不同控件的两个Dependency Property. 示例 效果图: 只使用C#代码: //获取slider1的ValueDep ...
- 【WPF系列】基础学习-WPF设计模式概览
引言 “设计模式”这个让程序员提起来就“酷”的东东,着实让让初学设计模式的programer自我陶醉一番.太多的经历,告诉我们“凡事都要个度,要学会适可而止”,否则过犹不及.“设计模式”也一样,切莫为 ...
- [WPF系列]从基础起步学习系列计划
引言 WPF技术已经算不什么新技术,一搜一大把关于WPF基础甚至高级的内容.之前工作中一直使用winform所以一直没有深入学习WPF,这次因项目中使用了WPF技术来实现比较酷的展示界面.我在这里只是 ...
- 【WPF系列】基础学习-XAML
引言 WPF框架中已经提到,WPF框架提供XAML基本服务.WPF中XAML的引入向开发者提供UI设计和代码分离的编程型.XAML是WPF中提出的一个具有重要意义的新技术,基本涉及WPF中所有UI开发 ...
- [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板
引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate) 切换模板的两种方式: 使用DataTemplateSelecto ...
随机推荐
- Nancy之Pipelines三兄弟(Before After OnError)
一.简单描述 Before:如果返回null,拦截器将主动权转给路由:如果返回Response对象,则路由不起作用. After : 没有返回值,可以在这里修改或替换当前的Response. OnEr ...
- gRPC C#学习
前些天gRPC 发布1.0 版本,代表着gRPC 已经正式进入稳定阶段. 今天我们就来学习gRPC C# .而且目前也已经支持.NET Core 可以实现完美跨平台. 传统的.NET 可以通过Mono ...
- html自我设计login登录
效果图如下: 第一次设计出能看的前台,就当留作纪念吧.
- IT基础架构规划方案三(IT基础软件和系统规划)
IT基础软件和系统规划 操作系统选型规划方案 根据对某集团的实际调研,获取了企业业务应用系统的建设情况,随着企业信息化建设的推进,需要对各种信息化管理系统和应用系统的服务器选型进行选型规划,根据不同的 ...
- Linux Ctrl+c与ctrl+z的区别
提问:CTRL-Z和CTRL-C区别?回答:CTRL-Z和CTRL-C都是中断命令,但是他们的作用却不一样.CTRL-C是强制中断程序的执行,而CTRL-Z的是将任务中断,但是此任务并没有结束,他仍然 ...
- JavaScript判断变量值简单的方法
今天在看一个动态web表单设计器的时候发现项目中的 一个写法 function sum_total(v){ if (!v) { v= 0; } } !v 这是什么写法?不过可以肯定的是,这是一种判断 ...
- SharePoint 2013 图文开发系列之自定义字段
SharePoint使用的优势,就在于开箱即用.快速搭建,SharePoint自身为我们提供了很多字段类型,已经很丰富了.但是,在实际应用中,我们还需要一些功能特殊的字段,下面,我们简单介绍下字段的开 ...
- Upgrade from SharePoint 2010 to SharePoint 2016
[转]http://nikcharlebois.com/upgrade-from-sharepoint-2010-to-sharepoint-2016/ In this blog, I will go ...
- 自定义View字段表头
适用场景: 三个列表进行Join,然后试图上显示ProjectedField,而ProjectedField不支持设置DisplayName.默认只能显示英文名. join caml如下: <V ...
- eclipse启动时报错 Could not create the java virtual machine
eclipse启动的时候 报错 这是系统为eclipse分配的内存不足,需要去修改 eclipse.ini文件 eclipse.ini是eclipse 内存分配之类的配置文件 对java虚拟机的 ...