[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 ...
随机推荐
- 利用JsonConvert.SerializeObject()实现类对象的json化
现阶段的项目是采用前后端分离的思想,前端使用的是Angular.JS,后端使用ABP框架,在后端我们通过WebAPI技术来向前端提供json数据.以前是通过MVC来写前端的代码,感觉后端有点在控制前端 ...
- 解决WebApi入参时多对象的问题
我们的项目是用WebApi提供数据服务,且WebPage跟APP中都有调用到. WebApi提供的接口一多,就发现一个问题,我们项目中有很多接口是接收POST(安全原因,我们采用的是https)请求的 ...
- .net中创建xml文件的两种方法
.net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...
- Redis和Memcached整体
Redis和Memcached整体对比 Redis的作者Salvatore Sanfilippo曾经对这两种基于内存的数据存储系统进行过比较,总体来看还是比较客观的,现总结如下: 1)性能对比:由于R ...
- Java并发编程:Thread类的使用
Java并发编程:Thread类的使用 在前面2篇文章分别讲到了线程和进程的由来.以及如何在Java中怎么创建线程和进程.今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知 ...
- JDK源码分析:hashCode()方法
提问: 1.hashCode()源码是怎么实现的. 2.hashCode()是为了配合基于散列的集合而设计的 3.hash数据结构,如何做到存取的时间复杂度为O(1)的.{函数算>逐个比较} 答 ...
- 原生JS实战:写了个斗牛游戏,分享给大家一起玩!
本文是苏福的原创文章,转载请注明出处:苏福CNblog:http://www.cnblogs.com/susufufu/p/5869953.html 该程序是本人的个人作品,写的不好,未经本人允许,请 ...
- 一些有用的SAP技术TCODE
Background Processing RZ01 Job Scheduling Monitor SM36 Schedule Background Job SM36WIZ Job definitio ...
- 一句话知识:如何解决winform自动缩放产生的布局问题.
转自http://www.cnblogs.com/KenBlove/articles/1281823.html有时候你会发现本来好好的WinForm程序在别的机器上显示的尺寸就不对了.这些问题主要发生 ...
- 用UILocalNotification实现一个闹钟(Swift)
之前项目需求要实现一个闹钟,github上找了半天发现都是很旧的代码了,所以就准备自己写一个,刚好最近在学习Swift,就用Swift写了一个demo放在这里:https://github.com/P ...