I have written a MVVM prototype as a learning exercise and I am struggling to understand how I can communicate between views.Let me explain
I have a treeview on the left (leftSideView)
ListView on the right (rightSideView)
MainWindow (includes the 2 views mentioned above and a splitter)
What I have implemented does not work and I would like you if you can point out where I am going wrong or if there is a better way of doing it. you can download the quick prototype from here
http://cid-9db5ae91a2948485.skydrive.live.com/self.aspx/.Public/WpfCommunicationBetweenViews.zip
For sure I am doing something wrong with the bindind. Also it would be nice to learn how you do it. EG ListBox on left (one view) ListView On the right (another view) how u communicate between the two.
Thanks a lot of any suggestions
I took a look at your code, made the modifications shown below, and it worked. I changed the right side view to just have a textblock to simplify it a bit.
MainWindow.xaml.cs (Create a view model for both views to bind to)
LeftSideView.xaml.cs (set the data context of this view to be the view model and update the selected city of the view model when changed)
RightSideView.xaml.cs (set the right side view to use the same view model)
In the RightSideView.xaml, I just put the textbox that is shown below:
When a city on the left view is selected, it will changed the selected city on the view model, therefore, it will updated the selected city name on the right view.
Here's what the ProtoViewModel class looked like:
I would share the modified files with you, but I'm not sure how to do that :)
You might also consider using a loosely-coupled Mediator pattern. See for example:
Laurent Bugnion's Mediator in the MVVM Light Toolkit here
MVVM Foundation's Messenger here
Prism's Event Aggregator here
Josh Twist's ISuckLessEventAggregator here