richzilla
richzilla

Reputation: 41992

Calling property changed on a bound element

My view model implements INotifyPropertyChanged for properties that it makes available to my view. It makes available a collection of objects that do not implement INotifyPropertyChanged.

My collection is bound to an ItemControl in my view, with an ItemTemplate used to display each item invidually. The item template is bound to the Name attribute of my collection members.

How can i tell my view to update when properties of my collection members change?

Upvotes: 0

Views: 494

Answers (2)

Rachel
Rachel

Reputation: 132548

You need to either implement INotifyPropertyChanged for the objects in your collection (recommend approach), or you can manually refresh the binding by something like

myItemsControl.GetBindingExpression(
     ItemsControl.ItemsSourceProperty).UpdateTarget();

If you're in the ViewModel, you might be able to raise a PropertyChanged event on your Collection class such the following, although I am not sure if that will update the individual items or not

// My PropertyChanged method is usually called RaisePropertyChanged
RaisePropertyChanged("MyCollection");  

You can also do what Mirimon suggeted and set the value to null then back again, although personally I would recommend a different approach if possible.

Upvotes: 1

Mirimon
Mirimon

Reputation: 102

You must implement INotifyPropertyChanged for collection members. Or you can reset your collection in ViewModel:

public void Reset() {
    List<TestData> temp = YourCollection;
    YourCollection = null;
    YourCollection = temp;
}

Upvotes: 1

Related Questions