-2
我使用帶有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}";
}
}
}
瞭解。但是,當在WPF窗口中使用相同的代碼時,如果我明確調用View.Refresh()方法或將IOT作爲ItemsSource連接到列表框。 – msedi
請提供您的問題的WPF示例。 – mm8