Is ViewModelLocator supposed to remove neccessity of DataTemplate?
I am sure I am missing something simple (still a bit green on this mvvm stuff), but I thought that the use of ViewModelLocator removed the need of DataTemplate binding view to viewmodel. But for 开发者_开发知识库some reason I seem to still need it.
In a WPF application I have a window whose only content is a content template which binds to default binding.
<Window ... DataContext="{Binding MainView, Source={StaticResource Locator}}">
<grid><ContentControl Content="{Binding}"/></grid>
I then have a UserControl which I guess you could say is the real view - viewmodel.
<UserControl ...
DataContext="{Binding MainView, Source={StaticResource Locator}}">
...xaml...
</UserControl>
What I have noticed is that unless I place DataTemplate which binds view - viewmodel (MainView in this case) in App.xaml, I just get the name of the class.
Since the window's datacontext is bound using the locator, I thought this would work. My guess is that the additional layer in the window using the ContentControl is confusing things.
If I replace the ContentControl with a direct reference to the view, ie.
<view:MainView />
This also works. So I guess I have two questions: 1. Why does the binding not seem to understand this? 2. Is there a way to get this to work using the locator? I would like to have the window with minimal ui, and keep the bulk in UserControls.
Thank you for any information.
Obscured
each content control can hold any object, if you just assign class without DataTemplate, this is by default translated to string. DataTemplate says how the content should be visualized, including bindings (which propeties of your object should be taken and evaluated).
ViewModelLocator doesn't have anything with DataTemplates, it just locates the right view model for you based on name or whatever you want. It is the way how to decouple logic of creating/locating view models and put this at one place.
精彩评论