I've been learning the MVVM pattern with Josh Smith's article and I want to create a classic layout with some links to the right (managed with commands) so when I click one I can show my view to the right into a tab control (inside it there is a ContentControl).
This is simple when I use a DataTemplate with the specific View and ViewModel I wan开发者_如何转开发t to show on screen like this.
<!-- this section into my MainWindow's resources file -->
<DataTemplate xmlns:vm='clr-namespace:WpfFramework.ViewModels'
xmlns:vw='clr-namespace:WpfFramework.Views'
DataType="{x:Type vm:MySpecificViewModel }" >
<vw:MySpecificView />
</DataTemplate>
But, I want something more generic. I mean that my mainWindow should not know a specific View nor a specific ViewModel. It should only know that it binds to some commands and has a tab control which shows "some view". Every sample including Josh Smith's article seems to have limited universe of views and viewmodels, that's great with a sample.
So, how can I tell my ContentControl that some view (with its corresponding viewModel) is gonna be there without being so specific (without "burning" into the mainView the concrete types)?
best regards Rodrigo
PD. I have tryed with base a ViewModel and Base View but it doesn't seem to work.
In your main View, bind a ContentControl
to a generic ViewModelBase
property
<ContentControl Content="{Binding CurrentPage}" />
CurrentPage
would be defined in the main ViewModel as a ViewModelBase
object, and to switch pages you simply set CurrentPage
to whatever you want.
So when you click on something like the HomePageCommand
, the main ViewModel would execute CurrentPage = new HomePageViewModel();
providing that HomePageViewModel
inherits from ViewModelBase
.
I wrote something a little while ago that shows some samples here if you're interested
精彩评论