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的更多相关文章

  1. [WPF系列]-基础系列 Property Trigger, DataTrigger & EventTrigger

    So far, we worked with styles by setting a static value for a specific property. However, using trig ...

  2. [WPF系列]-基础系列 TabControl应用

    引言 Tabcontrol控件也是我们在项目中经常用到的一个控件,用它将相关的信息组织在一起分类显示. 简介     ========================================= ...

  3. [WPF系列]基础学习(一) WPF是什么?

    引言 学习之前,我们首先大概了解下WPF诞生的背景以及它所能解决的问题或者新颖之处.WPF作为微软新一代的用户界面技术,   WPF简介 WPF的全称是WindowsPresentationFound ...

  4. [WPF系列]基础Combox

    示例     参考 WPF combobox SelectedValue binding to string Confused with wpf ComboBox DisplayMemberPath, ...

  5. [WPF系列]-使用Binding来同步不同控件的Dependency property

    简介 项目中经常会用到,同步两个控件的值,本文就简单列举两种方式来同步不同控件的两个Dependency Property. 示例 效果图: 只使用C#代码: //获取slider1的ValueDep ...

  6. 【WPF系列】基础学习-WPF设计模式概览

    引言 “设计模式”这个让程序员提起来就“酷”的东东,着实让让初学设计模式的programer自我陶醉一番.太多的经历,告诉我们“凡事都要个度,要学会适可而止”,否则过犹不及.“设计模式”也一样,切莫为 ...

  7. [WPF系列]从基础起步学习系列计划

    引言 WPF技术已经算不什么新技术,一搜一大把关于WPF基础甚至高级的内容.之前工作中一直使用winform所以一直没有深入学习WPF,这次因项目中使用了WPF技术来实现比较酷的展示界面.我在这里只是 ...

  8. 【WPF系列】基础学习-XAML

    引言 WPF框架中已经提到,WPF框架提供XAML基本服务.WPF中XAML的引入向开发者提供UI设计和代码分离的编程型.XAML是WPF中提出的一个具有重要意义的新技术,基本涉及WPF中所有UI开发 ...

  9. [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板

      引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate)   切换模板的两种方式:   使用DataTemplateSelecto ...

随机推荐

  1. asp.net WebService+Ajax调用

    default.aspx 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  2. C#判断字符串是否是数字

    /// <summary> /// 判断字符串是否是数字 /// </summary> public static bool IsNumber(string s) { if ( ...

  3. 异步编程系列第02章 你有什么理由使用Async异步编程

    p { display: block; margin: 3px 0 0 0; } --> 写在前面 在学异步,有位园友推荐了<async in C#5.0>,没找到中文版,恰巧也想提 ...

  4. 把cookie以json形式返回,用js来set cookie.(解决手机浏览器未知情况下获取不到cookie)

    .继上一篇随笔,链接点我,解决手机端cookie的问题. .上次用cookie+redis实现了session,并且手机浏览器可能回传cookies有问题,所以最后用js取出cookie跟在请求的ur ...

  5. C# 之 DataReader 和 DataSet 的区别

    本文转载自:http://www.cnblogs.com/xinaixia/p/4920630.html 1. 获取数据的方式[1]DataReader 为在线操作数据, DataReader会一直占 ...

  6. N-Tier Entity Framework开源项目介绍

               N-Tier Entity Framework是一个基于微软Entity Framework的N层.NET解决方案. 并且与以下这此技术点无缝集成了: § WCF RIA Ser ...

  7. jquery动态改变div宽度和高度

    效果体验:http://keleyi.com/keleyi/phtml/jquery/23.htm 完整代码: <!DOCTYPE html PUBLIC "-//W3C//DTD X ...

  8. 兼容 IE input button 左右内边距的增加

    IE 下的按钮标签(input.button)会随着文字的增加导致文本左右两侧的间距越来越大.该问题存在于 IE6/IE7,IE8~IE10 没发现类似问题.一般情况下,只要给按钮标签设置 overf ...

  9. iOS之数据持久化方案

    概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...

  10. Linux常用命令:文件与目录

    目录与路径 cd:切换目录 例如:cd ~willhua,则回到用户willhua的主文件夹  cd ~或者cd,则表示回到自己的的主文件夹  cd -,则表示回到上个目录 pwd:显示目前所在目录 ...