简体   繁体   English

MultiDataTrigger 与具有多重绑定的 DataTrigger

[英]MultiDataTrigger vs DataTrigger with multibinding

I encountered a situation where I can easily achieve the same functionality by using a MultiDataTrigger or, alternately, using a DataTrigger with a MultiBinding .我遇到过这样一种情况,我可以通过使用MultiDataTrigger或使用带有MultiBindingDataTrigger轻松实现相同的功能。 Are there any substantive reasons to prefer one approach over the other?是否有任何实质性理由偏爱一种方法而不是另一种方法?

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>

Multibinding requires a converter for all but the rarest circumstances (using StringFormat ). Multibinding情况外(使用StringFormat ), Multibinding需要转换器。

MultiTrigger only requires a converter to get your binding results into boolean s. MultiTrigger只需要一个转换器即可将您的绑定结果转换为boolean s。

I would like to elaborate a little bit more.我想再详细说明一点。

For me, MultiBinding and MultiDataTrigger are 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 same way.对我来说, MultiBindingMultiDataTrigger是根本不同的,虽然在某些情况下您可以使用两者来实现相同的功能,但让两者以相同的方式工作感觉有点像黑客。

MultiDataTrigger s should be used when you need more than one condition to be met separately so that you can do an action (set a property value, begin an animation etc).当您需要单独满足多个条件时,应该使用MultiDataTrigger以便您可以执行操作(设置属性值,开始动画等)。 For example, you need A to be true and B to be false.例如,您需要A为真, B为假。 Both of these conditions can by themselves be interpreted separately.这两个条件本身都可以单独解释。 It's the case of this question.这个问题就是这种情况。

MultiBinding s, on the other hand, should be used when you need more than one parameter to calculate a single output of your choice.另一方面,当您需要多个参数来计算您选择的单个输出时,应该使用MultiBinding s。 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 A equals B .例如,如果A等于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".当您在多个控件上使用相同的样式并且 A 是控件的一个属性(例如 TextBlock 的 Text 属性)而 B 是视图模型中名为“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).所以我们可能要解决的一个问题是:在我的视图上的所有 TextBlocks 中,将与我的视图模型中的 SelectedText 属性具有相同文本的文本块的前景设置为闪烁(颜色变化动画)。

In your example, I would use a MultiDataTrigger since your conditions can be evaluated separately.在您的示例中,我将使用MultiDataTrigger因为您的条件可以单独评估。 Otherwise your MultiValueConverter would only check for your second condition, ignoring the first one and would serve no real purpose for being a Multi DataTrigger really.否则,您的 MultiValueConverter 只会检查您的第二个条件,而忽略第一个条件,并且不会真正用作Multi DataTrigger。

I'll leave the XAML for the example where I'd use the DataTrigger with MultiBinding that 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM