绑定到同一VM属性的多个DataTriggers的问题

时间:2016-01-21 22:26:21

标签: wpf xaml

当多个DataTriggers绑定到同一个ViewModel属性时,我遇到了一个问题。经过调查,我注意到最后一个赢了。我该如何克服这个问题?感谢

          <Style.Triggers>

                <DataTrigger Binding="{Binding Path=ShowGrid}" Value="false">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="50"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>

                <DataTrigger Binding="{Binding Path=ShowGrid}" Value="true">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)">
                                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                                    <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="50"/>
                                </DoubleAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>

            </Style.Triggers>

顺便说一下,这很好用:

                <DataTrigger Binding="{Binding Path=ShowGrid}" Value="false">
                    <Setter Property="Height" Value="0"></Setter>
                </DataTrigger>

                <DataTrigger Binding="{Binding Path=ShowGrid}" Value="true">
                    <Setter Property="Height" Value="100"></Setter>
                </DataTrigger>

那么,它必须与故事板相关,有任何线索吗?

1 个答案:

答案 0 :(得分:0)

故事板的FillBehavior默认为HoldEnd,将其更改为FillBehavior=Stop

默认行为是保持故事板设置的最后一个值,告诉它在完成后重置它们。

修改

好的,现在我明白了你的意思。您已使用DataTrigger.ExitActions:

<DataTrigger Binding="{Binding Path=ShowGrid}" Value="true">
    <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)">
                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="50"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)">
                    <EasingDoubleKeyFrame KeyTime="0" Value="50"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.ExitActions>
</DataTrigger>

试过它,它似乎做你想要的。别忘了删除其他DataTrigger。