WPF:纯XAML午餐时的情节提要绑定条件

时间:2018-07-11 03:26:34

标签: wpf storyboard

所以我有这个Storyboard

<Storyboard x:Key="animate">
            <DoubleAnimation BeginTime="0:0:0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:2.0"/>
</Storyboard>

我的绑定value

public bool IsFound
{
    get { return _isFound; }
    set
    {
        _isFound= value;
        NotifyPropertyChanged();
    }
}

还有我的Grid得到这个Storyboard

<Grid name="myGrid">
   ....
<Grid>


if(IsFound)
{
    Storyboard storyboard = Resources["animate"] as Storyboard;
        if (storyboard != null)
            storyboard.Begin(myGrid);
}

所以我正在寻找纯XAML的东西,而不是在后面的代码中检查此IsFound

1 个答案:

答案 0 :(得分:1)

您可以使用DataTrigger:

<Style TargetType="Grid" x:Key="MyAnimatedGrid">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsFound}" Value="True">
            <DataTrigger.EnterActions>
                <BeginStoryboard StoryBoard="{StaticResource animate}" />
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>