CollectionViewSource:LiveShaping属性不起作用
问题描述:
我使用带有LiveShaping属性的CollectionViewSource,尤其是激活分组。问题在于更改分组属性不能立即生效。但是,当我通过在视图上手动调用刷新来刷新视图时,它可以正常工作。CollectionViewSource:LiveShaping属性不起作用
但问题是,为什么我在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}";
}
}
}
答
一个CollectionViewSource
启用在控制台应用程序那里是可用同样的原因没有调度员作为答案被提及到的情况下无法正常工作现场分组您的其他问题:
了解。但是,当在WPF窗口中使用相同的代码时,如果我明确调用View.Refresh()方法或将IOT作为ItemsSource连接到列表框。 – msedi
请提供您的问题的WPF示例。 – mm8