DataTrigger
ControlTemplate
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},
Path=(ui:TreeViewItemHeader.IsDragTarget)}"
Value="True">
<Setter TargetName="PART_Content" Property="Foreground" Value="Red" />
</DataTrigger>
绑定属性IsDragTarget是一个附加属性,它被编码到自定义控件类中:
public static readonly DependencyProperty IsDragTargetProperty = DependencyProperty.RegisterAttached
("IsDragTarget",
typeof(bool),
typeof(TreeViewItemHeader),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetIsDragTarget(DependencyObject self, bool value)
{
self.SetValue(IsDragTargetProperty, value);
}
public static bool GetIsDragTarget(DependencyObject self)
{
return (bool)self.GetValue(IsDragTargetProperty);
}
可能会描述一个问题,即当控件初始化时,DataTrigger
只会出现一次解析值。更新属性值不会翻转DataTrigger。
执行了故障排除:
IValueConverter
“返回值”已添加到绑定中; IValueConverter.Convert
方法只被调用一次。DataTrigger.Value
属性设置为False
- 附加属性的初始值;当控件初始化时,Setters会触发一次。对该属性的后续更改未触发DataTrigger。答案 0 :(得分:3)
使用DataTrigger
是否严格要求?我已经通过切换到Trigger
:
<Trigger Property="ui:TreeViewItemHeader.IsDragTarget" Value="True">
<Setter TargetName="PART_Content" Property="Foreground" Value="Red" />
</Trigger>
不知道为什么DataTrigger
无效。
答案 1 :(得分:0)
您需要通过实现INotifyPropertyChanged接口
来提升属性值更改事件如here
所示答案 2 :(得分:0)
不应该是吸气剂&amp; setter的定义如下:
public static readonly DependencyProperty IsDragTargetProperty = DependencyProperty.RegisterAttached
("IsDragTarget",
typeof(bool),
typeof(TreeViewItemHeader),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
public bool IsDragTarget {
get { return (bool)this.GetValue(IsDragTargetProperty); }
set { this.SetValue(IsDragTargetProperty, value); }
}