DataTrigger上的故事板未正确触发

时间:2016-09-13 17:30:04

标签: wpf xaml data-binding

我正在努力创建一个"下拉"使用Storyboard上的StackPanel自定义菜单的动画。我在我的ViewModel(实现DataTrigger)中的布尔值上使用INotifyPropertyChanged来实际触发BeginStoryboard。目前,其中一个Storyboard的火灾(Value="False"),但另一个火灾没有。示例代码如下。

<DockPanel>
    <Button DockPanel.Dock="Top" Command="{Binding TogglePanelVisibleCommand}"/>
    <StackPanel Orientation="Vertical">
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsPanelVisible}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="350" Duration="0:0:0.45" />
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsPanelVisible}" Value="False">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetProperty="Height" From="350" To="0" Duration="0:0:0.45" />
                                </Storyboard>
                            </BeginStoryboard>
                        </DataTrigger.EnterActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>
        <Button Content="Button 1"/>
        <Button Content="Button 2"/>
        <Button Content="Button 3"/>
        <Button Content="Button 4"/>
    </StackPanel>
</DockPanel>

基本上,我希望顶部按钮在按下时切换下拉动作。该命令只是切换后面代码中的位。我需要布尔值在后面的代码中,这样我也可以从ViewModel代码的其他UI元素切换菜单。

有谁知道为什么Storyboard中只有一个实际上正在运行?

编辑:对于好奇,这里是命令的实现和ViewModel布尔值。

private bool _IsPanelVisible = false;
public bool IsPanelVisible
{
    get { return this._IsPanelVisible; }
    set
    {
         this._IsPanelVisible = value;
         this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsPanelVisible"));
    }
}

public ICommand TogglePanelVisibleCommand = new RelayCommand(o => this.IsPanelVisible = !this.IsPanelVisible);

1 个答案:

答案 0 :(得分:3)

你可以尝试使用它吗?

<DataTrigger.EnterActions>
    <BeginStoryboard Name="OpenDropDown">
        <Storyboard>
            <DoubleAnimation Storyboard.TargetProperty="Height" From="0" To="350" Duration="0:0:0.45" />
        </Storyboard>
    </BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
    <RemoveStoryboard BeginStoryboardName="OpenDropDown" />
</DataTrigger.ExitActions>