I have a child window and there is a ComboBox. I want to send value of child window to parent window using WPF and MVVM. Can anyone help me how to do it ??
-
can you help us in telling what have you tried? – MZaragoza Jan 04 '15 at 04:05
3 Answers
Don't focus on MVVM for this scenario, MVVM is not designed for passing values from child windows back to parent windows.
Instead, if you have some code in a viewmodel or the code behind of a view that spawns or opens the child window, then that code should be responsible for retrieving the value determined by the child window and propagating it back to the appropriate property (at which point any binding will reflect the value back in the UI - this is where MVVM should be used).
The best thing you can do is make sure the code that opens the child window doesn't go directly accessing a ComboBox on the child window, instead the child window should bind it to a property which is then accessed by the parent's code (preferably via an interface).
For further reading check out Creating an MVVM friendly dialog strategy. This should be your preferred solution, then the parent code simply uses the Dialog Service to show the child window, and the Dialog Service is responsible for aggregating the child window result and making it available back to the calling code in the parent window.
Since you havent given adequate info so, lets assume the child window is a dialog. Now, lets assume the child window is a class Child() with its ViewModel having the object in Child class, say
public ChildViewModel chVM { get; set; }
and this viewmodel having the property:
public string ComboBoxSelectedValue { get; set; }
Lets have the xaml of the dialog having the combobox as this :-
<ComboBox Name="cbTest" SelectedItem="{Binding ComboBoxSelectedValue}">
<ComboBoxItem>A</ComboBoxItem>
<ComboBoxItem>B</ComboBoxItem>
<ComboBoxItem>C</ComboBoxItem>
</ComboBox>
Now, everytime a value is selected in the combobnox, the property in its viewmodel that is, ComboBoxSelectedValue will be filled with the selected value.
You have to handle the Close event of the dialog on your parent page. Lets move on to the class Parent() that is the parent page:
public partial class Parent : Page
{
private Child ch;
public Parent()
{
InitializeComponent();
ch= new Child();
ch.Closed += ChildClosed;
}
public void ChildClosed(object sender, System.EventArgs e)
{
//even after closing of child window
var selectedValue = ch.chVM.ComboBoxSelectedValue;
}
public void OpenChild(object sender, System.EventArgs e)
{
//Button event to open the child window
ch.Show();
}
}
Please respond if this is the one you needed. Or else please feel free to ask for another solution. This can be done without mvvm also, but since you asked for MVVM, so this is the solution.

- 183
- 10
-
While this is an option, it is a bad approach - you are reaching deep into the guts of the viewmodel via the view. The parent should only know about the immediate object it creates - anything more is breaking several OO and/or design rules. If you open the view, it's okay to know about that - but to make assumptions about the viewmodel and its properties is bad. The viewmodel could be *anything* - this is one of the fundamentals of MVVM. If you are going to do this then at least interface things. – slugster Jan 04 '15 at 11:56
The best solution I have found for allowing view models to communicate with each other is via a messaging framework, my preference is MVVM light available on nuget.
Your child view model sends a message via the framework, which the parent subscribes to. Not dissimilar to event handlers.
Child...
Messenger.Default.Send<MyMessageClass>(message);
That can go in a combo box selected item binding setter, or part of a command action method.
Parent...
Messenger.Default.Register<MyMessageClass>(this, OnMessage);
The MyMessageClass must extend BaseMessage and should include properties for the data you want to share. The OnMessage method in the parent should accept this class as a parameter and do whatever you need it to in the parent with those values.
Is is better MVVM since it keeps the logic out of code behind or view, but also doesn't create strong couplings. If a view model sends a message that no other view model receives, nothing happens - you also use message objects rather than inspection of view models to share data.
Tutorial on msdn here.

- 3,423
- 2
- 16
- 28
-
1This is a bad approach - messaging is for when you have multiple recipients who may or may not be active that need to be notified of a change - for example a setting changed that can affect multiple different windows. Messaging is also not MVVM and leads to maintenance issues when you get a proliferation of possible messages. Yes it is decoupled, but for a parent/child dialog scenario messaging is massive overkill. – slugster Jan 04 '15 at 11:49
-
I'm inclined to disagree and offer this as another alternative to those suggested based on not knowing the specifics of the implementation. It is intended for where there are zero to many recipients and done properly, does not present any real maintenance. I will concede that unit testing can become a nuisance but for extensibility and decoupling I think it works well. This is of course opinion and you are entitled to yours. I would add that if looking at a true parent child model where the child and parent views are intrinsically linked, share a single instance of the same view model. – kidshaw Jan 04 '15 at 12:02