Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight Gridview - Update Groups upon property change
    text
    copied!<p>Suppose we have a simple class with two string properties, implementing INPC:</p> <pre><code> public class TestClass : INotifyPropertyChanged { private string _category; public string Category { get { return _category; } set { _category = value; NotifyPropertyChanged("Category"); } } private string _name; public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged("Name"); } } private void NotifyPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>Let's create an ObservableCollection of these and assign them to a PagedCollectionView:</p> <pre><code>public PagedCollectionView MyItems { get; set; } public void Test() { var items = new ObservableCollection&lt;TestClass&gt;(); items.Add(new TestClass { Category = "A", Name = "Item 1" }); items.Add(new TestClass { Category = "A", Name = "Item 2" }); items.Add(new TestClass { Category = "B", Name = "Item 3" }); items.Add(new TestClass { Category = "B", Name = "Item 4" }); items.Add(new TestClass { Category = "C", Name = "Item 5" }); items.Add(new TestClass { Category = "C", Name = "Item 6" }); items.Add(new TestClass { Category = "C", Name = "Item 7" }); MyItems = new PagedCollectionView(items); MyItems.GroupDescriptions.Add(new PropertyGroupDescription("Category")); } </code></pre> <ul> <li>Note that we have also set up a grouping on category</li> </ul> <p>Then, using MVVM we bind this up in our XAML:</p> <pre><code>&lt;sdk:DataGrid ItemsSource="{Binding Path=MyItems}" /&gt; </code></pre> <p>If we then go in and edit one of the categories, say changing one of the "C"s to "A" for example, the gridview handles it brilliantly. It maintains collapse status of groups, and even adds new group headers if needed!</p> <p>The problem occurs when we change the category programmatically in the viewmodel (or, for example, from within another gridview bound to the same data). In this case, the <em>category text</em> will updated, but the item will not move into the new appropriate group, new groups will not be created, row headers will not be updated, etc.</p> <p>How can I trigger the gridview to update its groups when the property is changed outside of the gridviews editing capabilities?</p> <p>Any workaround is welcome, however triggering a simple Refresh() is not as that will wipe out scrolling/collapsing/etc. </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