WPF: Condition Binding versus Property, XamlParseException using either

Sarah Vessels picture Sarah Vessels · Jan 10, 2011 · Viewed 10.3k times · Source

I'm having trouble with a Condition for a MultiTrigger. If I do the following:

<Condition Binding="{Binding RelativeSource={RelativeSource
    AncestorType={x:Type ListView}}}" Property="IsEnabled" Value="True"/>

Then I get this exception:

Condition cannot use both Property and Binding. Error at object 'System.Windows.Condition' in markup file

However, when I do the following:

<Condition Binding="{Binding RelativeSource={RelativeSource
    AncestorType={x:Type ListView}}, Path=IsEnabled}" Value="True"/>

Then I get this exception:

Must specify both Property and Value for Trigger. Error at object 'System.Windows.Condition' in markup file

What gives? If it matters, here's the entire trigger:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Binding="{Binding Path=IsSelected}" Value="True"/>
        <Condition Binding="{Binding Path=ItemsControl.AlternationIndex}"
                   Value="0"/>
        <Condition Binding="{Binding RelativeSource={RelativeSource
            AncestorType={x:Type ListView}}, Path=IsEnabled}"
                   Value="True"/>
    </MultiTrigger.Conditions>
    <Setter Property="Background"
            Value="{StaticResource evenSelected}" />
    <Setter Property="BorderBrush"
            Value="{StaticResource evenSelectedBorder}" />
</MultiTrigger>

Answer

Abe Heidebrecht picture Abe Heidebrecht · Jan 10, 2011

The API in this case is confusing. Condition is used for two different types of multi-triggers, and the properties used are different. When using MultiTrigger, you will use the Property and Value properties. When using MultiDataTrigger (which is what you need), you specify a Binding and a Value. So, if you just switch your code to use a MultiDataTrigger, you'll be good to go:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Path=IsSelected}" Value="True"/>
        <Condition Binding="{Binding Path=ItemsControl.AlternationIndex}"
                   Value="0"/>
        <Condition Binding="{Binding RelativeSource={RelativeSource
            AncestorType={x:Type ListView}}, Path=IsEnabled}"
                   Value="True"/>
    </MultiDataTrigger.Conditions>
    <Setter Property="Background"
            Value="{StaticResource evenSelected}" />
    <Setter Property="BorderBrush"
            Value="{StaticResource evenSelectedBorder}" />
</MultiDataTrigger>