WPF - 没有回调的依赖属性的兴趣是什么

时间:2015-01-06 16:53:20

标签: wpf dependency-properties

我没有理解没有定义回调的依赖属性的兴趣。

1 个答案:

答案 0 :(得分:0)

回调只是一个额外的便利 - 依赖属性被集成到框架运行时,并具有内置的回调机制,可以更新任何绑定。也就是说,如果使用依赖项属性设置绑定作为源,则目标将在依赖项属性更改时自动更新。

例如,假设您有一个定义了DP的自定义控件:

public string SomeDP
{
    get { return (string)GetValue(SomeDPProperty); }
    set { SetValue(SomeDPProperty, value); }
}
public static readonly DependencyProperty SomeDPProperty =
    DependencyProperty.Register("SomeDP", typeof(string), typeof(SomeFrameworkElement), new PropertyMetadata(null));

如果你设置一个与DP的绑定作为TextBlock"文本"的源。属性:

<local:SomeFrameworkElement x:Name="someFrameworkElement" SomeDP="initial" />
<TextBlock Text="{Binding ElementName=someFrameworkElement,Path=SomeDP}" />

然后每当&#34; someFrameworkElement&#34;&#34; SomeDP&#34;属性发生变化,文本也会发生变化。