WPF依赖属性值源(BaseValueSource)
2010-08-03 13:26 Clingingboy 阅读(1752) 评论(0) 编辑 收藏 举报
WPF依赖属性提供一个机制,可以获取依赖属性提供值的来源
其以BaseValueSource枚举表示
1.Default
public static readonly DependencyProperty WorkingTagProperty = DependencyProperty.Register( "WorkingTag", typeof(object), typeof(CustomControl), new UIPropertyMetadata("<working tag>"));
2.DefaultStyle和DefaultStyleTrigger(Style和StyleTrigger)
注意点:若即定义在外部程序集中的默认控件样式则为DefaultStyle和DefaultStyleTrigger,否则定义在本地项目(如在Application中重新定义控件样式,则为Style和StyleTrigger)
3.TemplateTrigger和ParentTemplateTrigger
在模板中触发
<ControlTemplate TargetType="{x:Type local:CustomControl}"> <Button x:Name="Part_Button" Content="{TemplateBinding Title}" IsEnabled="{TemplateBinding IsEnabled}" Background="{TemplateBinding Background}" /> <ControlTemplate.Triggers> <Trigger SourceName="Part_Button" Property="IsEnabled" Value="False"> <Setter Property="Title" Value="disable test text"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>
4.ParentTemplate
当模板中有定义内部控件样式模板时,内部控件值变更由上级模板触发
下面TextBlock的Text的触发源为ParentTemplate
<Style TargetType="{x:Type local:CustomControl}"> <Setter Property="Title" Value="default test text" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomControl}"> <ControlTemplate.Resources> <Style TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <TextBlock Text="{TemplateBinding Content}"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </ControlTemplate.Resources> <Border> <Button x:Name="Part_Button" Content="{TemplateBinding Title}" IsEnabled="{TemplateBinding IsEnabled}" Background="{TemplateBinding Background}" /> </Border> <ControlTemplate.Triggers> <Trigger SourceName="Part_Button" Property="IsEnabled" Value="False"> <Setter Property="Title" Value="disable test text"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
5.Local
即本地调用了SetValue方法
public object WorkingTag { get { return GetValue(WorkingTagProperty); } set { SetValue(WorkingTagProperty, value); } }
6.Inherited
属性继承是一个向下包容继承的关系
由属性继承值更改触发,如文字大小(FontSize)就是一个继承关系,更改根元素,那么子元素就全会更改
此功能也可以用来debug用,用来检测值是由何时修改的.
.net 4.0新增方法
DependencyObject ..::. SetCurrentValue 方法
这个方法类似SetValue方法,但SetCurrentValue 不会去修改属性值源(即何处修改属性值)
参考文档:
http://msdn.microsoft.com/en-us/library/ms743230.aspx
参考绑定时bug
http://blogs.msdn.com/b/vinsibal/archive/2009/03/24/the-control-local-values-bug.aspx
http://arbel.net/blog/archive/2009/11/04/local-values-in-dependencyobjects.aspx