How to bind view textbox to parent viewmodel?

Topics: Conventions
Nov 1, 2012 at 7:42 PM

I have a viewmodel called EmployeeDetailViewModel.  This viewmodel fetches data into a property named Model.  The viewmodel also has an importmany of IEmployeeDetailView, called Items.  The EmployeeDetailView has a tabcontrol named Items. 

The EmployeeDetailView displays and the tabcontrol displays each of the imported IEmployeeDetailView as a different tab.

But I would like the view's for each IEmployeeDetailView to bind their control values to the Model property on EmployeeDetailViewModel.  Is that possible?

Nov 2, 2012 at 12:13 AM
Edited Nov 2, 2012 at 12:29 AM

The follwoing code would meet my needs if there is a way to utilize this code to work on a tab control to generate tab items?  In this test, MyControl is a string on the VM and I'd like it to be a collection of strings.

  <ContentControl cal:View.Context="{Binding MyControl, Mode=TwoWay}"



Nov 2, 2012 at 11:51 PM
Edited Nov 3, 2012 at 12:41 AM

What is your file structure?

Views are in folder called views?

if that is the case then you would create a subfolder called EmployeeDetail, in that folder you would create another UserControl (name it DefaultView.xaml, for example) wire up all xmlns as needed to make the control work with CM.  Now your string property MyControl would be assigned the value "DefaultView", now when your app runs that view for EmployeeDetail will have the "DefaultView" as the context.

In your viewmodel is where you would call MyControl = "DefaultView":

in DefaultView you can wire up controls with names associated to Model... x:Name="Model_SomeProperty"  this will "deep link" your data in TwoWay mode or you can do it the non-convention way as expected through normal bindings

This is one way of doing view composition in CM

Nov 5, 2012 at 11:50 PM

Thanks for the feedback.  I decided on a direction of creating views and viewmodels for each of the parts.  I created a base class that each part inherits.  This base class subscribes to events from the EmployeeDetailViewModel whenever the Model property changes and stores a reference to the Model.  Everything seems to be working properly and feels very clean.  What I'm not sure about is how storing several references to the same Model (i'm going to have several parts which make up how the Model is viewed) is going to impact memory and resources.