WPF - 没有回调的依赖属性有什么好处
WPF - What's the interest of dependency property without callback
我不明白没有定义回调的依赖属性的意义。
回调只是一个额外的便利——依赖属性被集成到框架运行时中,并且有一个内置的回调机制来更新任何绑定。也就是说,如果您将具有依赖项 属性 的绑定设置为源,则目标将在依赖项 属性 更改时自动更新。
例如,假设您有一个定义了 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 的源 "Text" 属性:
<local:SomeFrameworkElement x:Name="someFrameworkElement" SomeDP="initial" />
<TextBlock Text="{Binding ElementName=someFrameworkElement,Path=SomeDP}" />
那么每当"someFrameworkElement"的"SomeDP"属性改变时,文本也会改变。
我不明白没有定义回调的依赖属性的意义。
回调只是一个额外的便利——依赖属性被集成到框架运行时中,并且有一个内置的回调机制来更新任何绑定。也就是说,如果您将具有依赖项 属性 的绑定设置为源,则目标将在依赖项 属性 更改时自动更新。
例如,假设您有一个定义了 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 的源 "Text" 属性:
<local:SomeFrameworkElement x:Name="someFrameworkElement" SomeDP="initial" />
<TextBlock Text="{Binding ElementName=someFrameworkElement,Path=SomeDP}" />
那么每当"someFrameworkElement"的"SomeDP"属性改变时,文本也会改变。