WPF: Condition Binding versus Property, XamlParseException using either

对着背影说爱祢 提交于 2019-12-01 17:27:14

问题


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>

回答1:


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>


来源:https://stackoverflow.com/questions/4649906/wpf-condition-binding-versus-property-xamlparseexception-using-either

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!