我正在使用具有LiveShaping属性的CollectionViewSource,尤其是已激活分组。问题是更改分组属性不会立即生效。但是,当我通过在View上手动调用Refresh来刷新视图时,它可以工作。
但问题是,如果它们不起作用,为什么我在CollectionViewSource上有LiveShaping属性?或者我做错了什么?
以下是一个例子:
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}";
}
}
}
答案 0 :(得分:0)
启用了实时分组的CollectionViewSource
在控制台应用程序的上下文中不起作用,在该应用程序中没有可用的调度程序,原因与您在其他问题的答案中提到的相同: