2008-09-10 73 views
19

在WPF中,DataTrigger是否可以绑定到附加属性?你可以将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 path error: 'Validation' property not found on 'object' ''TextBox' (Name='')'. BindingExpression:Path=Validation.Errors; DataItem='TextBox' (Name=''); target element is 'TextBox' (Name=''); target property is 'NoTarget' (type 'Object')

如果我更改了DataTrigger约束力的路径“文本”,我没有得到数据绑定错误(但它当然不提供我正在寻找的行为)。

回答

27

你需要用财产括号:

<DataTrigger Binding="{Binding Path=(Validation.Errors).YourAttachedProperty,... 
+4

这是在http://msdn.microsoft.com/en-us/library/ms752300.aspx#Path_Syntax – 2009-11-30 18:11:51

相关问题