如何使添加淡入/淡出动画基于视图模型的属性值?(How to make add a fade-in/

2019-06-25 06:39发布

我有一个公开的字符串属性PageToolBarVisible可以是 视图模型

private string _pageToolBarVisible;
public string PageToolBarVisible
{
    get
    {
        return _pageToolBarVisible;
    }

    set
    {
        _pageToolBarVisible = value;
        OnPropertyChanged("PageToolBarVisible");
    }
}

然后在我查看我有这个DataTrigger, 显示隐藏相应的工具栏:

<Style x:Key="PageToolBarStyle" TargetType="Border">
    <Style.Triggers>
        <DataTrigger Binding="{Binding PageToolBarVisible}" Value="false">
            <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

<Border Style="{StaticResource PageToolBarStyle}"
    DockPanel.Dock="Bottom" Padding="5 5 5 0" Background="#eee">
    <Grid Background="#eee">
        ...
    </Grid>
</Border>

我现在该如何添加动画 ,因此:

  • 当视图模型属性是从真到假的变化, 工具栏淡出
  • 当视图模型属性是从虚假到真实的改变, 工具栏淡入

我想我有这样的事情添加到我的风格,但我不知道如何或在哪里:

<BeginStoryboard>
<Storyboard>
    <DoubleAnimation
    Storyboard.TargetName="PageToolBar"
    Storyboard.TargetProperty="(TextBlock.Opacity)"
    From="0.0" To="1.0" Duration="0:0:3"/>
</Storyboard>
</BeginStoryboard>

Answer 1:

你可以把你的BeginStoryboard里面DataTrigger.EnterActions

<DataTrigger Binding="{Binding PageToolBarVisible}" Value="false">
    <DataTrigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                Storyboard.TargetName="PageToolBar"
                Storyboard.TargetProperty="(TextBlock.Opacity)"
                From="0.0" To="1.0" Duration="0:0:3"/>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.EnterActions>

    <DataTrigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation
                    Storyboard.TargetName="PageToolBar"
                    Storyboard.TargetProperty="(TextBlock.Opacity)"
                    From="1.0" To="0.0" Duration="0:0:3"/>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.ExitActions>
</DataTrigger>


文章来源: How to make add a fade-in/fade-out animation based on ViewModel property value?