WPF: pure XAML when lunch Storyboard base of binding condition

So i have this Storyboard:

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

My binding value:

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

And my Grid that get this Storyboard:

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


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

So i am looking for something pure XAML instead of checking this IsFound in code behind.

1 answer

  • answered 2018-07-11 03:33 Mark Feldman

    You can use a 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>