tags:

views:

443

answers:

2

I'm using MVVM in my WPF application.
I have a Window that has a view inside it. In that view a value can be changed and I will bind it to a TextBox in my main Window:

<TextBox
    Text="{Binding Path=myViewModel.MyValue">
</TextBox>

Now I would need to bind this myViewModel.MyValue to a property inside my main window view model, so that I can use it to perform a filter in a collection.

Can I use a Setter or any other thing that sets the value of my MainViewModel.Property whenever the myViewModel.MyValue changes?
If so, how can I do it?

A: 

Use binding again, this time from code. Take a look at the FrameworkElement.SetBinding member function.

Bruno Martinez
This is not something you'd do in an MVVM application. A ViewModel wouldn't have knowledge of the View and vice-versa.
Anderson Imes
+1  A: 

Does your "sub" view's view model live inside your "main" window's viewmodel? If so, you can simply trigger these things through the setters.

If not, you might be able to use an ancestor binding and drive this from the UI. It's not ideal (I'd assume you are trying to avoid the parent and child views from having specific knowledge of each other), but it would work.

Here's a RelativeSource sample that is pretty simple. These can get quite complex. Use with caution: http://www.charlespetzold.com/blog/2006/04/101102.html

If you do decide to go the RelativeSource route, you ought to know how to debug bindings. Use this link for that. She gives several techniques for this, but if you are using .NET 3.5, I'd recommend using that method (near the end of the article).

Anderson Imes