2008-09-10 6 views
19

Dans WPF, est-il possible pour un DataTrigger de se lier à une propriété jointe?Pouvez-vous lier un DataTrigger à une propriété attachée?

Je souhaite essentiellement utiliser un convertisseur sur une propriété jointe pour fournir un style lorsqu'une règle de validation particulière a été rompue. J'utilise le balisage comme ce qui suit:

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

Cependant, quand cela fonctionne, je reçois le texte suivant:

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

Si je change mon chemin contraignant DataTrigger à « Texte », je ne reçois pas la erreur de liaison de données (mais bien sûr, il ne fournit pas le comportement que je cherche).

Répondre

27

Vous devez envelopper la propriété entre parenthèses:

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

Ceci est documenté sur MSDN à http://msdn.microsoft.com/en-us/library/ms752300.aspx#Path_Syntax –

+0

@M. Dudley le lien ne fonctionne plus! –

+2

Nouveau lien: https://msdn.microsoft.com/library/ms752300%28v=vs.100%29.aspx#Path_Syntax –

Questions connexes