Using WPF my team is attempting to seperate the design and the code using the MVVM design patterns. To accomplish this, we are slowly moving away from the UserControl approach as this has a high ammount of coupling between design and code. We have been investigating using Control Templates, Styles and DataTemplates, in combination with a ViewModel class. We have gotten a large amount of this working to date.
The issues we are having are in relation to communications/notifications between View and ViewModel. At present, we have "solved" the View -> Viewmodel communication issues using ICommand. i.e. We create a button and bind its' "Command" parameter to the name of a RelayCommand defined in the ViewModel. By doing so, a button click or other Command event raised from the View calls a function defined in the ViewModel. This works.
Where we're having our main issue is in getting the notification to run the other way around: i.e. a change in data in the ViewModel needs to trigger an update in the View. We were doing this using NotifyPropertyChanged and DataTriggers, however this does not satisfy our needs. What we need is to be able to raise an event of some kind in the Viewmodel and have the View subscribe to said event. We have been searching for an answer to this and have found out about both RoutedEvents and AttachedBehaviors. RoutedEvents seemed like a winner of a solution, however from our research RoutedEvents can't be registered onto a ViewModel that doesn't extend from UIElement, and we are particularly trying to keep our code separated from the design.
Ultimately what we're trying to do is set up a ViewModel where a parameter can be set or a function can be called that will raise an event or behavior on the View, and subsequently run an animation. We did have this working with DataTriggers, however we are attempting to seperate our Animations out into the ControlTemplate and this raises issues as the DataTemplate that contains the DataTriggers has no access to the Storyboards defined in the ControlTemplate.
Can someone please point us in the right direction? Specifically the raising of an event in a ViewModel (that does not require extending UIElement) and subscribing to this event in the View and activating a Storyboard.
Thanks
I have found that MVVM isn't a very good choice for loose coupling. By default Microsoft Model-View-Whatever solution templates throw everything into one project. To be truly loosely coupled, try moving everything but the GUI elements into their own libraries and avoid all references to a specific target. A loosely coupled system would have the System.Windows or System.Web references only on the target (client/console/web serving) platform and have reusable components in model and business layer libraries. You will probably end up with some facade and adapter classes that facilitate generic implementations. I suppose it depends what you are trying to decouple. Frameworks in general add more restrictions. I know that it's not a very popular viewpoint, but that's my 2 cents.