Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had to handle this problem and although the 'Refresh()' solution works well, it is quite long to execute because its refreshes the whole list just for one item property changed event. Not very good. And in a scenario of real time data entering the collection every 1 seconds, I let you imagine the result in user experience if you use this approach :)</p> <p>I came up with a solution which base is : when adding an item to collection wrapped in a collectionview, then the item is evaluated by the filter predicate and, based on this result, displayed or not in the view.</p> <p>So instead of calling refresh(), I came up simulating an insert of the object that got its property updated. By simulating the insert of the object, it is going to be automatically evaluated by the filter predicate without need to refresh the whole list with a refresh.</p> <p>Here is the code in order to do that : </p> <p>The derived observable collection : </p> <pre><code>namespace dotnetexplorer.blog.com { using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; /// &lt;summary&gt; /// Derived class used to be able to manage filter application when a collection item property changed /// whithout having to do a refresh /// &lt;/summary&gt; internal sealed class CustomObservableCollection : ObservableCollection&lt;object&gt; { /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref = "CustomObservableCollection " /&gt; class. /// &lt;/summary&gt; public CustomObservableCollection () { } /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="CustomObservableCollection "/&gt; class. /// &lt;/summary&gt; /// &lt;param name="source"&gt; /// The source. /// &lt;/param&gt; public CustomObservableCollection (IEnumerable&lt;object&gt; source) : base(source) { } /// &lt;summary&gt; /// Custom Raise collection changed /// &lt;/summary&gt; /// &lt;param name="e"&gt; /// The notification action /// &lt;/param&gt; public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e) { OnCollectionChanged(e); } } } </code></pre> <p>And there is the code to use when receiveing item property changed event where substitute source is a CustomObservableCollection :</p> <pre><code> private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e) { // To avoid doing a refresh on a property change which would end in a very hawful user experience // we simulate a replace to the collection because the filter is automatically applied in this case int index = _substituteSource.IndexOf(sender); var argsReplace = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, new List&lt;object&gt; { sender }, new List&lt;object&gt; { sender }, index); _substituteSource.RaiseCollectionChanged(argsReplace); } } } </code></pre> <p>Hope this will help !</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