dytori
dytori

Reputation: 487

Synchronized child property in MVVM

Question is implementation pattern for the following.

A container UI has child UIs. For example, the container has a property of ObservableCollection<ChildItemViewModel> that is applied to <ChildItem DataContext="{Binding}" /> in XAML template.

I want a property of ChildItemViewModel synchronized among hosted ChildItems. As setting/unsetting PropertyChanged event seems messy, binding is preferable. An idea is that the parent UI has a dependency property bound in two-way to a corresponding dependency property (also bound to a property of the ChildItemViewModel) of every child UI in the template, which still seems redundant to prepare such dependency properties.

May I have your declarative and MVVM-natural way?

Upvotes: 0

Views: 244

Answers (1)

sidjames
sidjames

Reputation: 117

If what you are talking about is setting a property on the parent view model from it's child view model the way I usually do it is as follows:

  • Add a constructor to the child view model that takes the parent view model as a parameter and assign that to a field in the child view model.

  • Add a method to the parent view model to change the desired property.

  • Call method from child property (using the 'parent' field).

Hope that is helpful.

Upvotes: 1

Related Questions