views:

48

answers:

1

I can think of two ways to implement dependency properties that are shared between the detail views:

  1. Store them in the master view model and add data bindings to the detail view models when they are created, and bind to them in the detail view.
  2. Don't store them in the view models at all, and use FindAncestor to bind directly to properties of the master view instead.

What are the pros and cons of each, and are there other/better options?

Edit:

To clarify, I have a custom control (derived from Control) that uses a master view model for its DataContext. The custom control's control template contains an ItemsControl with ItemsSource bound to a dependency property in the master view model. This dependency property is an ObservableCollection of detail view model objects. The ItemsControl's item template binds to properties in the detail view model. What I need is a single property that is shared by the control template of the custom control (master view) and the item template for all the items in the ItemsControl. The custom control will contain a Slider or something to set the value, and the item template will simply read it.

Sorry if I'm abusing the terms, I'm still trying to get the hang of MVVM. If the problem still is unclear, I can try to write some code for a simple test case.

A: 

Implementing the dependency properties in the the View's code behind is the most popular practice I have seen. This allows you to interact with the DP via Databinding to the ViewModel. There aren't really any cons, DPs are meant to go one the object that needs to implement databinding.

If the above is not what you are asking, please include a brief Code Example to.

Agies