I’m new to C# and am trying to learn MVVM by building a simple Crud app. In my App I want to have a series of buttons that when the user clicks on it will load that user control. So when you click on the “Add Item” it will replace the existing control with the AddItemView. I was wondering how I go about this? A lot of the mvvm tutorials that I’ve seen so far they just have a single control on the MainMindow.
Share
For this kind of task, you typically use a
ContentControl. Bind theContentproperty of the control to a property of your ViewModel (CurrentViewModelfor instance), and defineDataTemplatesin the resources for each type of ViewModel that can be displayed in theContentControl. When you assign a ViewModel toCurrentViewModel, theContentControlwill pick the appropriateDataTemplatefor the content.Main ViewModel
App.xaml
Main view
In your case, in the command for the “Add item” button, you assign an instance of
AddItemViewModeltoCurrentViewModel