6
votes

I have a boolean property called IsLoginWrong, I want to then play a storyboard animation if the IsLoginWrong is true. (IsLoginWrong does an OnPropertyChanged event, so I know the binding is ok) But I'm having a hard time with the syntax. This might not even be right, but I think datatriggers can only live in Styles...

<UserControl.Style>
    <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsLoginWrong}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource LoginWrong}"/>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>

But this throws an exception "A storyboard tree in a Style cannot specify a TargetName"... beause styles canno refer to items specifically.. awesome. so how do I do what I'm trying to do? (play animation if a boolean changes in mvvm)

Thanks

2
Looking back at this 5 years later, mvvm was probably the biggest waste of time in my career. better to do this in code.Shai UI

2 Answers

13
votes

Within a style you cannot refer to a storyboard name. So the way I got it to work is to shove your storyboard within the actual style:

<UserControl.Style>     
    <Style>         
        <Style.Triggers>             
            <DataTrigger Binding="{Binding Path=IsLoginWrong}" Value="True">                       
                <DataTrigger.EnterActions>                     
                    <BeginStoryboard>
                        <Storyboard>
                            .... PUT YOUR ACTUAL STORY BOARD IN HERE ...
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>             
            </DataTrigger>         
        </Style.Triggers>     
    </Style> 
</UserControl.Style>

Now DataTriggers can either be put into styles or control templates, so there might be a nicer way to do this with control templates. but this is what I came up with for the time being.

2
votes

One option would be to start the storyboard using the VisualStateManager. The article at http://blogs.infosupport.com/blogs/alexb/archive/2010/04/02/silverlight-4-using-the-visualstatemanager-for-state-animations-with-mvvm.aspx explains how to control the current state of the VisualStateManager from the view model using an attached property.