How to subscribe to change DependencyProperty? [duplicate]

有些话、适合烂在心里 提交于 2019-12-21 07:34:29

问题


Possible Duplicate:
Listen to changes of dependency property

Excuse me for my English.

I need to create a class that could subscribe to change DependencyProperty, and depending on the new value of this property to perform some action.

Like this:

MyClass obj = new MyClass();
obj.Subscribe(TextBox.TextProperty, myTextBox);

How can I do this?


回答1:


Here is one way of doing it, using the handy DependencyPropertyDescriptor class.

 var pd = DependencyPropertyDescriptor.FromProperty(TextBox.TextProperty, typeof(TextBox));
 pd.AddValueChanged(myTextBox, OnTextChanged);


 private void OnTextChanged(object sender, EventArgs e)
 {
     ...
 }



回答2:


Take a look at this article. http://msdn.microsoft.com/en-us/magazine/cc794276.aspx#id0070111. It shows how to use DPDescriptor to get changed events



来源:https://stackoverflow.com/questions/8482923/how-to-subscribe-to-change-dependencyproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!