你能将DataTrigger绑定到一个附加属性上吗?

20

在WPF中,DataTrigger能否绑定到附加属性?

我希望在特定的验证规则被触发时,在附加属性上使用转换器来提供样式。我正在使用如下标记:

<DataTrigger Binding="{Binding Path=Validation.Errors, 
                       RelativeSource={RelativeSource Self}, 
                       Converter={StaticResource RequiredToBoolConverter}}" 
                       Value="True">
  <Setter Property="Background" Value="LightGreen" />
</DataTrigger>

然而,当我运行时,会出现以下错误:

System.Windows.Data Error: 39 : BindingExpression路径错误: 在'对象''TextBox' (Name='')'上未找到'Validation'属性。 BindingExpression:Path=Validation.Errors; DataItem='TextBox' (Name=''); target element is 'TextBox' (Name=''); target property is 'NoTarget' (type 'Object')

如果我将DataTrigger绑定路径更改为“Text”,则不会出现数据绑定错误(但当然它不会提供我所寻求的行为)。

1个回答

31
您需要使用括号包裹属性:
<DataTrigger Binding="{Binding Path=(Validation.Errors).YourAttachedProperty,...

4
此内容已在MSDN文档(http://msdn.microsoft.com/en-us/library/ms752300.aspx#Path_Syntax)中记录。 - sourcenouveau
3
新链接:https://msdn.microsoft.com/library/ms752300%28v=vs.100%29.aspx#Path_Syntax - sourcenouveau

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接