MVVM with Implicit DataTemplates - getting the View explicitly
I am using implicit DataTemplates of Silverlight 5 to wire ViewModel and View.
In my MainView there is a TabControl (from Telerik) which is bound to an ObservableCollection of the MainViewModel. And for all possible ViewModel-types within that Collection there is a DataTemplate which defines the corresponding View (=Tab).
Very clean and totally decoupled.
But now I need to write code that's really only related to the Views: I want to manually merge a Ribbon of the selected Tab开发者_JS百科 to the Ribbon of the MainView. The ViewModels don't have any knowledge of their corresponding View and do not know anything about a UI Element like a Ribbon (which is a good thing). So this is one of the very rare occasions where I need to write code-behind.
I tried the SelectionChanged Event of the TabControl, but all properties (SelectedItem, SelectedValue, ...) point to the ViewModel and not to the View.
How do I get the View - which was wired by Silverlight 5 using a DataTemplate??
Thanks for any ideas!
Short answer, you don't. Use an event aggregator or a messenger class with strongly typed constraints.
One good implementation with MVVM-Light is the Messenger class, see this article for info.
http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx
Anyhow you should be able to have the ribbon message the tab via messengers if i'm understanding you right.
精彩评论