Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a main window with a grid dividing the window in half horizontally.

In the upper window I have a couple of buttons and a datagrid that holds the directories I'm searching.

The bottom window has a clear button and a datagrid view that displays the results of the search.

How can I declare the SearchViewModel for the upper window and the ResultsViewModel for the bottom window in XAML?

As far as I can tell I can only declare one ViewModel in the main window XAML. Why can't I have a data context declared in XAML for each of the halves and bind each datagrid to their respective ViewModel?

I must be missing something basic here.

Thanks for your help.
Posted

Who told you that you can only create one view in a single window (and its XAML)?
This CodeProject article gives you the simple example of multiple views in one window: MVVMLight Using Two Views[^] (maybe not a very good article, but good enough for the purpose of out discussion).

Note, that I'm talking about multiple views, not multiple view models. Discussing mapping between view models and windows, as you are trying to do, is not a valid approach. View models are not visual things, views are, so first consider different views in one window, which is more than reasonable. After all, the application UI design with only one (non-modal) main window is one of the most convenient ones. You are not suggesting anything unusual.

As to the mapping between view models and views, this is a separate concern. Try to follow very reasonable discussion on multiple views and view models in this article: http://www.wintellect.com/devcenter/jlikness/model-view-viewmodel-mvvm-explained[^].

The author arguably insists that there is no need "to attach multiple viewmodels to a single view", but "a single viewmodel might be used by multiple views" — as part of "the chicken of the egg problem". Here, you also don't suggest anything unusual. As I could understand you, you want to have one view model for search view and one for the results view. It sounds quite reasonable.

—SA
 
Share this answer
 
As I understand it, I can only declare one instance of a viewmodel in the XAML? So, do I go to back code to create the instances?
 
Share this answer
 
Comments
Pete O'Hanlon 3-Sep-15 6:33am    
Err no - it's just convention that you have one instance, but there actually aren't any rules that you can't have multiple VM's in the XAML - the key thing to understand is that every item has it's own DataContext (that's inherited), so if you need to use a different DataContext then you just bind to the appropriate VM at that point. Of course, there are many different ways to solve this particular problem. If you use VM first resolution, then you would let WPF render in the appropriate Grid based off supplying a different VM. Alternatively, you would separate out the Grids into separate Views with their own VMs and compose them together into one master View. As I said, there are many, many ways to solve this - WPF provides a lot of flexibility here.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900