您的位置:首页 > 产品设计 > UI/UE

#146 – Use GetValueSource Method to Find the Source of a Dependency Property Value

2016-06-30 09:51 726 查看
使用GetValueSource 方法获得依赖属性值的来源
原文地址:https://wpf.2000things.com/2010/12/05/146-use-getvaluesource-method-to-find-the-source-of-a-dependency-property-value/

有时候能够确定依赖属性当前值的来源是非常有用的。你可以使用DependencyPropertyHelper.GetValueSource 方法实现。

在下面的例子中,Foreground 属性的值可以来自样式或者样式里定义的基于的IsEnabled 属性的触发器。

<Window.Resources>
<Style x:Key="redgreenButton" TargetType="{x:Type Button}">
<Setter Property="Foreground" Value="Green"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel Orientation="Vertical">
<Button Content="A Button" Height="23" Width="75" Style="{StaticResource redgreenButton}" Name="btnTest"/>
<Button Content="Enable/Disable" Height="24" Width="100" Name="btnDisable" Click="btnDisable_Click"/>
<Button Content="Display Source" Height="24" Width="100" Name="btnDisplay" Click="btnDisplay_Click"/>
</StackPanel>

下面是显示对话框按钮Click事件的实现,它使用GetValueSource 来报告属性的来源。

private void btnDisplay_Click(object sender, RoutedEventArgs e)
{
ValueSource vs = DependencyPropertyHelper.GetValueSource(btnTest as DependencyObject, Button.ForegroundProperty);
MessageBox.Show(string.Format("Source for Foreground property: {0}", vs.BaseValueSource));
}








内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WPF