I'm using the CollectionViewSource with LiveShaping properties, especially Grouping activated. The problem is that changing the Grouping properties does not work immediately. It works however, when I refresh the view by manually calling Refresh on the View.
But the question is then, why do I have the LiveShaping properties on the CollectionViewSource if they do not work? Or I'm I doing something wrong?
Here is an example:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Data;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
ObservableCollection<Test> TestList = new ObservableCollection<Test>();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Test t = new Test()
{
Group = i.ToString(),
Name = (j + (i * 10)).ToString()
};
TestList.Add(t);
}
}
CollectionViewSource aViewSource = new CollectionViewSource();
aViewSource.Source = TestList;
aViewSource.IsLiveGroupingRequested = true;
aViewSource.GroupDescriptions.Add(new PropertyGroupDescription("Group"));
aViewSource.LiveGroupingProperties.Add("Group");
foreach (CollectionViewGroup o in aViewSource.View.Groups)
{
Console.WriteLine(o.Name);
foreach (Test o2 in o.Items)Console.WriteLine(" " + o2.Name);
}
Console.WriteLine("----------------------------------------------");
TestList[4].Group = "4";
//aViewSource.View.Refresh(); When using this line it works.
foreach (CollectionViewGroup o in aViewSource.View.Groups)
{
Console.WriteLine(o.Name);
foreach (Test o2 in o.Items) Console.WriteLine(" " + o2.Name);
}
Console.WriteLine("----------------------------------------------");
foreach (Test test in TestList)
{
Console.WriteLine("{0}: {1}", test.Group, test.Name);
}
Console.WriteLine("----------------------------------------------");
Console.ReadKey();
}
}
public class Test : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string myName;
private string myGroup;
public string Name
{
get
{
return myName;
}
set
{
myName = value;
OnPropertyChanged();
}
}
public string Group
{
get
{
return myGroup;
}
set
{
myGroup = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName_in = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName_in));
}
public override string ToString()
{
return $"{Group}: {Name}";
}
}
}