2

I'm refactoring a silverlight LOB application to use MVVM..

On one of my controls i have a few animations which currently get triggered in the codebehind once the data is loaded..

This is view specific data and since the viewmodel is supposed to be somewhat headless (not dependant on view objects like the animations)..

Where should this code be?

Thanks

Daniel Upton
  • 5,561
  • 8
  • 41
  • 64

2 Answers2

2

A DataTrigger, a DataTrigger! My kingdom for a DataTrigger!

In WPF, you can trigger animations based on data in your ViewModel. In Silverlight, they use the VSM...I still haven't bothered to learn how this is done. Apparently the Blend Behavior SDK (which you can download separately) provides a DataTrigger approximation that fires of a VSM go to behavior based on a binding. This stackoverflow question (not the accepted answer but the one by Mike Post) shows how to do it...and a follow up answer shows how to do it without Blend.

Community
  • 1
  • 1
Michael Brown
  • 9,041
  • 1
  • 28
  • 37
  • I give you +1 for humor. Programmers need more of that! =D – Mike Christian Jun 09 '11 at 17:11
  • Good answer.. trying desperately to use VSM.. but the only problem is the control is a Grid.. and you cant set a template on a grid, Any suggestions? – Daniel Upton Jun 10 '11 at 09:37
  • You're trying to animate a Grid as in the Panel? Or are you more concerned with elements within the grid. For instance if you're trying to animate a control into view when the data is loaded just target that directly. If you're trying to animate the full grid, wrap it in a UserControl and target that. – Michael Brown Jun 10 '11 at 14:32
2

In short, you can rig up your animation trigger to a data trigger -- no code-behind needed. Other View technologies (not WPF or Silverlight) will require you to add a "data load complete" event to the ViewModel.

MVVM is simply MVP with WPF/Silverlight integration, to make signaling things like this easier. I believe WPF includes a data trigger, that may be used to trigger the animation. This allows you to forego adding a "data load complete" event to the ViewModel. You COULD add the event now, but you may be wasting your time, preparing the code for MVP compatibility that may never be needed.

The purpose of MVVM and MVP is to make the presentation view interchangeable. Consider how a WinForms, WinCE, text-only terminal (or even text to speech) View should inform the user the data are loaded. Each technology will behave differently. Therefore, the act of altering the user interface should reside in the View's code-behind, when not using WPF or Silverlight.

Mike Christian
  • 1,526
  • 1
  • 15
  • 27
  • +1 for a good explanation of why animation is a view concern and should be left in the view. – avanek Jun 09 '11 at 17:09
  • 1
    Definitely good answer...to add to what you're saying, today the requirement could be an animation, tomorrow it could be showing a previously hidden portion of the UI. The VM shouldn't care what happens just give enough information so that the View can respond.\ – Michael Brown Jun 09 '11 at 17:15