Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A bit late perhaps, but this may help other users so I'll post anyway...</p> <p>Updating the CollectionView.Filter based on a PropertyChanged event is not supported by the framework. There are a number of solutions around this.</p> <p>1) Implementing the IEditableObject interface on the objects inside your collection, and calling BeginEdit and EndEdit when changing the property on which the filter is based. You can read more about this on the Dr.WPF's excellent blog here : <a href="http://drwpf.com/blog/2008/10/20/itemscontrol-e-is-for-editable-collection/">Editable Collections by Dr.WPF</a></p> <p>2) Creating the following class and using the RefreshFilter function on the changed object.</p> <pre><code>public class FilteredObservableCollection&lt;T&gt; : ObservableCollection&lt;T&gt; { public void RefreshFilter(T changedobject) { OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, changedobject, changedobject)); } } </code></pre> <p>Example:</p> <pre><code>public class TestClass : INotifyPropertyChanged { private string _TestProp; public string TestProp { get{ return _TestProp; } set { _TestProp = value; RaisePropertyChanged("TestProp"); } } public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string propertyName) { var handler = this.PropertyChanged; if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); } } FilteredObservableCollection&lt;TestClass&gt; TestCollection = new FilteredObservableCollection&lt;TestClass&gt;(); void TestClass_PropertyChanged(object sender, PropertyChangedEventArgs e) { switch (e.PropertyName) { case "TestProp": TestCollection.RefreshFilter(sender as TestClass); break; } } </code></pre> <p>Subscribe to the PropertyChanged event of the TestClass object when you create it, but don't forget to unhook the eventhandler when the object gets removed, otherwise this may lead to memory leaks</p> <p>OR</p> <p>Inject the TestCollection into the TestClass and use the RefreshFilter function inside the TestProp setter. Anyhow, the magic here is worked by the NotifyCollectionChangedAction.Replace which updates the item entirely.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload