Multidatatrigger与datatrigger.为什么我的多数据触发器不起作用? [英] Multidatatrigger vs datatrigger. Why does my multidatatrigger not work?

查看:84
本文介绍了Multidatatrigger与datatrigger.为什么我的多数据触发器不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么这行不通:

I cannot figure out why this does not work:

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="Brush_GridBackground" TargetType="{x:Type Grid}">
        <Style.Triggers>
            <multidatatrigger>
                <multidatatrigger.conditions>
                    <condition binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="Red" />
                    <condition binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="White" />
                    <condition binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="Black" />
                    <condition binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="Blue" />
                    <condition binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="Green" />
                </multidatatrigger.conditions>
                <setter property="Background" value="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" />
            </multidatatrigger>
        </Style.Triggers>
    </Style>
</resourcedictionary>




当此方法有效时:




When this works:

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="Brush_GridBackground" TargetType="{x:Type Grid}">
        <Style.Triggers>
                <datatrigger binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="Red">
                <setter property="Background" value="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" />
            </datatrigger>
            <datatrigger binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="White">
                <setter property="Background" value="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" />
            </datatrigger>
            <datatrigger binding="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" value="Black">
                <setter property="Background" value="{Binding ElementName=cb_BackgroundColor, Path=SelectedItem.Tag}" />
            </datatrigger>
        </Style.Triggers>
    </Style>
</resourcedictionary>

推荐答案

您的MultiDataTrigger中的条件是和"在一起的,因此所有条件都必须为true,这在发布的示例中是不可能的.
在DataTrigger示例中,将对所有DataTrigger的值进行评估,而对所有True值都将设置值.

因此,将多个触发器组合在一起,如果它们设置了保存值,则列表中的最后一个触发器将获得它的方式.触发器的类型称为MultiDataTrigger/MultiTrigger具有条件的集合,必须将所有条件都评估为true才能设置触发器以设置任何值.
The conditions in your MultiDataTrigger are and''ed together thus all has to be true, which is not possible in the posted example.
While in the DataTrigger example all the DataTrigger''s gets evaluated and any that is true gets to set values.

So multiple triggers are or''ed together, if they set the save value the last one on the list gets it''s way. And the type of trigger called MultiDataTrigger/MultiTrigger has a collection of conditions which must all be evaluated to true for the trigger to set any value(s).


这篇关于Multidatatrigger与datatrigger.为什么我的多数据触发器不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆