wpf MultiDataTrigger 与具有多重绑定的 DataTrigger

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20993293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:02:45  来源:igfitidea点击:

MultiDataTrigger vs DataTrigger with multibinding

wpfdatatriggermultibindingmultitrigger

提问by mcwyrm

I encountered a situation where I can easily achieve the same functionality by using a MultiDataTriggeror, alternately, using a DataTriggerwith a MultiBinding. Are there any substantive reasons to prefer one approach over the other?

我遇到过这样一种情况,我可以通过使用 aMultiDataTrigger或交替使用 aDataTrigger和 a来轻松实现相同的功能MultiBinding。是否有任何实质性理由偏爱一种方法而不是另一种方法?

With MultiDataTrigger:

使用 MultiDataTrigger:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding Path=SomePath}" Value="SomeValue"/>
        <Condition Binding="{Binding Path=SomeOtherPath, Converter={StaticResource SomeConverter}}" Value="SomeOtherValue"/>
    </MultiDataTrigger.Conditions>
    <MultiDataTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource MyStoryboard}"/>
    </MultiDataTrigger.EnterActions>
</MultiDataTrigger>

With MultiBinding:

使用多重绑定:

<DataTrigger Value="foo">
    <DataTrigger.Binding>
        <MultiBinding Converter="{StaticResource fooConv}"/>
            <Binding Path=SomePath/>
            <Binding Path=SomeOtherPath/>
        </MultiBinding>
    </DataTrigger.Binding>
    <DataTrigger.EnterActions>
        <BeginStoryboard Storyboard="{StaticResource MyStoryboard}"/>
    </DataTrigger.EnterActions>
</DataTrigger>

采纳答案by Gusdor

Multibindingrequires a converter for all but the rarest circumstances (using StringFormat).

Multibinding除了最罕见的情况(使用StringFormat)之外的所有情况都需要转换器。

MultiTriggeronly requires a converter to get your binding results into booleans.

MultiTrigger只需要一个转换器就可以将您的绑定结果转换为booleans。

回答by Daniel Marques

This is an old question, but I would like to elaborate a little bit more.

这是一个老问题,但我想详细说明一点。

For me, MultiBindingand MultiDataTriggerare fundamentally different and although in some situations you can use both to achieve the same functionality, it feels kind of like a hack to make both work the sameway.

对我来说,MultiBindingMultiDataTrigger是根本不同的,虽然在某些情况下,你可以同时使用来实现相同的功能,那感觉有点像一个黑客,使双方的工作相同的方式。

MultiDataTriggers should be used when you need more than one condition to be met separatelyso that you can do an action (set a property value, begin an animation etc). For example, you need Ato be true and Bto be false. Both of these conditions can by themselves be interpreted separately. It's the case of this question.

MultiDataTrigger当您需要单独满足多个条件以便您可以执行操作(设置属性值、开始动画等)时,应该使用 s 。例如,您需要A为真,也需要为B假。这两个条件本身都可以单独解释。这个问题就是这种情况。

MultiBindings, on the other hand, should be used when you need more than one parameter to calculate a single output of your choice. This output would need to be of some value for you to set the property. For example, you will only change the property value if Aequals B. This makes sense when you use the same style on multiple controls and A is a property of the control (say, the Text property of a TextBlock) and B is a single property from the View Model named "SelectedText". So a problem we might be trying to solve is this: among all the TextBlocks on my View, set the foreground of the one with the same Text as the property SelectedText from my View Model to blink (color changing animation).

MultiBinding另一方面,当您需要多个参数来计算您选择的单个输出时,应该使用 s。此输出需要具有一定的价值才能设置该属性。例如,您将仅在Aequals 时更改属性值B。当您在多个控件上使用相同的样式并且 A 是控件的一个属性(例如 TextBlock 的 Text 属性)而 B 是视图模型中名为“SelectedText”的单个属性时,这是有意义的。所以我们可能要解决的一个问题是:在我的视图上的所有 TextBlocks 中,将与我的视图模型中的 SelectedText 属性具有相同文本的文本块的前景设置为闪烁(颜色变化动画)。

In your example, I would use a MultiDataTriggersince your conditions can be evaluated separately. Otherwise your MultiValueConverter would only check for your second condition, ignoring the first one and would serve no real purpose for being a MultiDataTrigger really.

在您的示例中,我将使用 aMultiDataTrigger因为您的条件可以单独评估。否则,您的 MultiValueConverter 只会检查您的第二个条件,而忽略第一个条件,并且不会真正用作MultiDataTrigger。

I'll leave the XAML for the example where I'd use the DataTriggerwith MultiBindingthat I mentioned above: (I assume you are using the MVVM pattern)

我将离开XAML的例子我用DataTriggerMultiBinding,我上面提到的:(我假设你使用的是MVVM模式)

<Style TargetType="{x:Type TextBlock}" x:Key="SelectedTextStyle">
    <Setter Property="FontFamily" Value="Segoe UI Light"/>
    <Setter Property="FontSize" Value="24"/>
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
               <MultiBinding Converter="{StaticResource StringsToBooleanConverter}">
                    <Binding Path="SelectedText"/> <!--This is a property of the View Model-->
                    <Binding RelativeSource="{RelativeSource Self}" Path="Text"/> <!--This is the Dependency Property 'Text' of the TextBlock control-->
                </MultiBinding>
            </DataTrigger.Binding>
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="Foreground.Color" Duration="0:0:2" From="Black" To="DarkOrange" AutoReverse="True" FillBehavior="HoldEnd" RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation Storyboard.TargetProperty="Foreground.Color" Duration="0:0:0" From="DarkOrange" To="Black" FillBehavior="HoldEnd"/>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.ExitActions>
        </DataTrigger>
    </Style.Triggers>
</Style>