问题
I have a couple of Storyboards in my view that I would like to trigger from the ViewModel if possible. Is there a simple way or elegant way of doing this. Here is what I am trying to do.
Person Clicks on a Button-->RelayCommand (In the ViewModel), the Relay Command should then play the storyboard. Also one more thing, I would like to also trigger the storyboard animation by itself in the ViewModel without any interaction.
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding ButtonPress}" CommandParameterValue="RedButtonLight">
</cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
回答1:
I know it's a long time ago. But I've written a detailed blog post about Triggering Storyboards and MVVM.
http://mark.mymonster.nl/2010/12/14/trigger-a-storyboard-on-viewmodel-changes/
回答2:
If the button click is purely to power a view-related thing and isn't doing any actual application logic, then I would argue that you can do all this in the code-behind of the view class.
If this isn't the case then I would use a property on the Presentation (ViewModel) to signal that the Presentation is in a state, and have the view react to the PropertyChanged event and start the storyboard. This is assuming you are implimenting INotifyPropertyChanged on your Presentation class.
回答3:
Have a look at the expression samples. There is a trigger for events from the datacontext. DataEventTrigger
You could use that to trigger a ControlStoryboardAction
to start the story board whenever your viewmodel raises a particular event.
Your viewmodel could then raise the event as part of the command as well as at other times.
回答4:
Heres how you can do it in blend without touching a line of xaml or code : http://www.basarat.com/2011/05/expression-blend-starting-storyboard.html
来源:https://stackoverflow.com/questions/2548291/mvvm-trigger-storyboard-in-the-view-model-in-silverlight