I have MainWindow
and AddEdit
UserControl
. Inside MainWindow
I render this AddEdit like <Views:AddEditData />
, previous this namespace is added to Window element:
xmlns:Views="clr-namespace:MyProject.WPF.Views"
+++++++++++++++ ++++++++++++++++
ListOfData + + DataDetails +
+ + +
DataOne + + Name: txtBox1+
DataTwo + + +
DataThree + + +
+ + Save data +
+++++++++++++++ ++++++++++++++++
When user selects data on left side (DataTwo for example) I want to display it's properties (for simplicity only Name property) inside AddEdit user control (DataDetails panel).
Since this UserControl
is stored separately from the MainWindow should I use same MainWindowViewModel and same datacontext or should I create separated ViewModel for AddEdit UserControl
?
Hopefully this sounds clear, if not please ask for details.
Part 1. Display the properties of the control in MVVM
As I said in comments:
ViewModel
is not directly associated with aView
, so just refer to the name of the control would not be right. It would be better to set a property in theModel
, and bind it intoView
viaViewModel
, but the propertyName
does not support Binding (quote from the MSDN):so I suggest to use the
Tag
property orUid
. In my example (give an below), I useUid
property for these purposes.Part 2. Communication via ViewModels (pattern Mediator)
There are several embodiments of the Mediator pattern, but I like the most the implementation by
XAML Guy
, it is simple and clear - The Mediator Pattern.Implementation code
To demonstrate his work, I created a small example, which consists of two
Views
, each has its ownViewModel
andModel
.The project structure is shown below:
Output
When you click on Button, ListOfData
ViewModel
communicates via mediator with DataDetailsViewModel
, thus:All procedures that interact with the properties must register their
ViewModel
like this:In the example I used a
DataTemplate
insteadUserControl
. Below are the main part of the project:MainWindow.xaml
Models
DataDetailsModel
ListOfDataModel
ViewModels
DataDetailsViewModel
ListOfDataViewModel
Views
DataDetailsView
ListOfDataView
This project is available at this link.
As UserControl is maintained separately and not a part of Window content. I would suggest to have separate ViewModel.
Benefits of having separate ViewModel:
Reusability - In future if you want to do some changes in data related to UserControl (may be some logic change), all you have to go to your ViewModel and update it and it will get reflected across all windows. You don't have to worry to go to each Window's view model and update code.
Testability - In case you want to test logic related to your control (data part me talking here not view part), you can write it in isolation. No need to worry about testing of Window view model code.
Loosely Coupled - More than one people can work in isolation. Say one developer have to update some code related to Main window and other have to update some code related to UserControl. With one ViewModel in place, there will be some overlap and they can't work in isolation since there are dependent on other person to do its work before he/she can plug in his/her code in ViewModel.
Also check out here for communication between different ViewModels as you might need that to communicate between Window view model and User Control View model to pass selected data in left window.