mor*_*anu 9 wpf dependency-properties notify dependencyobject
我有一个名为MyComponent的类,它有一个DependencyProperty caled BackgroundProperty.
public class MyComponent
{
public MyBackground Background
{
get { return (MyBackground)GetValue(BackgroundProperty); }
set { SetValue(BackgroundProperty, value); }
}
public static readonly DependencyProperty BackgroundProperty =
DependencyProperty.Register("Background", typeof(MyBackground),
typeof(MyComponent), new FrameworkPropertyMetadata(default(MyBackground), new PropertyChangedCallback(OnPropertyChanged)));
}
Run Code Online (Sandbox Code Playgroud)
MyBackground是一个派生自DependencyObject的类,它有一些DependencyProperties.
public class MyBackground : DependencyObject
{
public Color BaseColor
{
set { SetValue(BaseColorProperty, value); }
get { return (Color)GetValue(BaseColorProperty); }
}
public static readonly DependencyProperty BaseColorProperty =
DependencyProperty.Register("BaseColor", typeof(Color),
typeof(MyBackground ), new UIPropertyMetadata(Colors.White));
[...]
}
Run Code Online (Sandbox Code Playgroud)
现在,我想要的是当MyBackground中的属性发生更改时,MyComponent将被通知MyBackground已更改,并且名为OnPropertyChanged的PropertyChangedCallback将被调用.
听起来您想使用 DependencyPropertyDescriptor 和 AddValueChanged。
这是一篇关于它的文章:http://www.codeproject.com/Articles/34741/Change-Notification-for-Dependency-Properties.aspx
..可能还有更好的实现:http://agsmith.wordpress.com/2008/04/07/propertydescriptor-addvaluechanged-alternative/
| 归档时间: |
|
| 查看次数: |
8892 次 |
| 最近记录: |