MVVM- Trigger Storyboard in the View Model in Silverlight

若如初见. 提交于 2020-01-02 08:53:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!