#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 属性的触发器。
下面是显示对话框按钮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));
}
![](https://2000thingswpf.files.wordpress.com/2010/11/style.png?w=630)
![](https://2000thingswpf.files.wordpress.com/2010/11/styletrigger.png?w=630)
原文地址: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));
}
![](https://2000thingswpf.files.wordpress.com/2010/11/style.png?w=630)
![](https://2000thingswpf.files.wordpress.com/2010/11/styletrigger.png?w=630)
相关文章推荐
- C#及WPF获取本机所有字体和颜色的方法
- WPF MVVM示例讲解
- WPF实现时钟特效
- PowerShell中调用WPF生成炫酷窗口实例
- WPF实现类似360安全卫士界面的程序源码分享
- C#中WPF使用多线程调用窗体组件的方法
- WPF实现渐变淡入淡出的登陆窗口效果
- WPF的数据绑定详细介绍
- WPF绑定实例详解
- 关于WPF使用MultiConverter控制Button状态的详细介绍
- 如何搭建新的WPF项目框架
- 超炫酷的WPF实现Loading控件效果
- 解析WPF实现音频文件循环顺序播放的解决方法
- WPF Toolkit - February 2010 Release
- 使用WPF创建一个Digg桌面应用
- Barcode Professional for WPF
- WPF数据表格支持vs2010
- Silverlight Gantt甘特图银光,WPF,甘特图,Silverlight,Gantt,任务甘特图,资源甘特图
- WPF甘特图开发包(含任务甘特图和资源甘特图)
- WPF设置窗体最前端显示