Can you bind a DataTrigger to an Attached Property?
Asked Answered
G

1

20

In WPF, is it possible for a DataTrigger to bind to an attached property?

I essentially want to use a converter on an attached property to provide a style when a particular validation rule has been broken. I am using markup like the following:

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

However, when this runs, I get the following:

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')

If I change my DataTrigger binding path to "Text", I do not get the databinding error (but of course it does not provide the behaviour I am seeking).

Git answered 10/9, 2008 at 1:47 Comment(0)
V
31

You need to wrap the property in parentheses:

<DataTrigger Binding="{Binding Path=(Validation.Errors).YourAttachedProperty,...
Verruca answered 10/9, 2008 at 6:51 Comment(2)
This is documented on MSDN at msdn.microsoft.com/en-us/library/ms752300.aspx#Path_SyntaxRoseola
New link: msdn.microsoft.com/library/…Roseola

© 2022 - 2024 — McMap. All rights reserved.