0

I am trying to figure out on how to trigger the PropertyChangedEvent when the middle layer of my binding changes. I will start with an example here:

public class MainViewModel :NotificationObject // Main DataContext { public SubViewModel SubVM{get; {_subVM = value; RaisePropertyChanged("SubVM");}} // observable property public void DoChangeSubVM() { SubVM = new SubViewModel(); // doing this will not update the egControl } } public class SubViewModel : NotificationObject { public Sub2ViewModel Sub2VM {get; set{_sub2VM = value; RaisePropertyChanged("Sub2VM");}} // observable property } public class Sub2ViewModel : NotificationObject { public int SomeProp {get; set {_someProp = value; RaisePropertyChanged("SomeProp");} // observable property } 

in the XAML:

<EgControl name="egControl" Content={Binding SubVM.Sub2VM.SomeProp} /> 

Now if I change the Sub2VM Property the egControl doesn't automagically get updated with the SomeProp value of the new Sub2VM instance. How does someone go about achieving this, with out manually having to raise all the Sub2ViewModel propertychanged events from Sub2VM property setter?

Using: Prism .NET 4.0

2
  • 1
    The value is supposed to change. You're probably doing something wrong, but there's no to tell unless you post your actual code. Commented May 15, 2012 at 19:46
  • You are right. It does do it by default. I had code where a observable property is accessing another op in a child object (different from the question I posted). I fixed it by subscribing to the child object property changed and raising the main property changed event. Thanks! Commented May 16, 2012 at 1:07

3 Answers 3

1

How does someone go about achieving this, with out manually having to raise all the Sub2ViewModel propertychanged events from Sub2VM property setter?

Answer

You have several possibilities:

Raise all property changed events in the setter, which you said you wanted to avoid. But it's a valid strategy to consider. If you know which properties are dependant on the results of another, then they will need to raise property changed in the setter for many properties.

public class myViewModel { private string _FirstName public string FirstName { get { return_FirstName }; set { _FirstName = value; RaisePropertyChanged("FirstName"); RaisePropertyChanged("FullName"); } } } 

Raise all property changed events in the method, after the new ViewModel has been constructed.

public class myViewModel { private string _FirstName public string FirstName { get { return_FirstName }; set { _FirstName = value; RaisePropertyChanged("FirstName"); } } public void UpdateFirstName(string firstName) { _FirstName = firstName; RaisePropertyChanged("FirstName"); RaisePropertyChanged("FullName"); } } 

Use the setters to set some properties, thus triggering the already present property changed event.

public class myViewModel { private string _FirstName public string FirstName { get { return_FirstName }; set { _FirstName = value; RaisePropertyChanged("FirstName"); } } public Person ClonePerson(Person rootPerson) { Person clone = new Person() { FirstName = rootPerson.FirstName; LastName = rootPerson.LastName; } return clone; } } 

Make a method that raises all property changed events, and call it in edge cases where you need to raise multiple changed events.

public class myViewModel { private string _FirstName public string FirstName { get { return_FirstName }; set { _FirstName = value; this.RaiseAllPropertyChanges(); } } public void RaiseAllPropertyChanges() { RaisePropertyChanged("FirstName"); RaisePropertyChanged("FullName"); } } 

The end result is this: For any bound UI element to know that it must update, the property changed event for that property must be raised.

Sign up to request clarification or add additional context in comments.

Comments

0

Okay, not sure about Prism, but generally speaking all three classes should be implementing property change notification. The simplist way to do so is with INotifyPropertyChanged. So SubViewModel should be more like:

public class SubViewModel : INotifyPropertyChanged { private Sub2ViewModel sub2vm; public Sub2ViewModel Sub2VM { get { return sub2vm; } set { sub2vm = value; OnPropertyChanged("Sub2VM"); } } protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; } 

Without property change notification, the UI doesn't know when to udpate a bound property.

1 Comment

All those classes inherit from NotificationObject, which implements the INotifyPropertyChanged object. I will update the example to be more specific
0

One way to go about it is to create a constructor for both the SubViewModel and Sub2ViewModel that initializes all the properties to some default value. This will ensure that your properties are initialized and give you the ability to set the initial values.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.