C# 排序可观测集合
假设我有employee类的C# 排序可观测集合,c#,.net,wpf,sorting,observablecollection,C#,.net,Wpf,Sorting,Observablecollection,假设我有employee类的可观察集合 public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>(); public class Employee { public string FirstName { get; set; } public string LastName { get; set; } public do
可观察集合
public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>();
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public double MobileNumber { get; set; }
public string City { get; set; }
public int Age { get; set; }
public Employee() {}
}
public observetecollection employeeCollection=new observetecollection();
公营雇员
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共双移动枚举器{get;set;}
公共字符串City{get;set;}
公共整数{get;set;}
公共雇员(){}
}
现在,我正在尝试对可观察集合(“employeeCollection”)进行排序
通过用户从组合框中的适当选择[将是….按名字排序….按MobileNumber排序等]
并且需要返回已排序的可观察集合….
意味着它不应该是“var”的形式,应该是
observedcollection
因此,我可以将其分配回“ItemsControl”
的“ItemsSource”
属性
谢谢……您可以对收藏视图进行排序,而不是对收藏本身进行排序:
// xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
<myView.Resources>
<CollectionViewSource x:Key="ItemListViewSource" Source="{Binding Itemlist}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="SortingProperty" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</myView.Resources>
您不需要自己排序,但可以让WPF为您排序。例如,请参阅。我认为PVitt可能有最好的解决方案。。。然而,我确实发现了这个
SortedObservableCollection类可能有帮助吗
我实现了一个ObservableCollectionView
,它支持使用lambda(如LINQ但live)和项目跟踪进行排序和过滤:
为什么要对集合进行排序?您还可以对数据绑定执行排序。[在我的应用程序“Item controle”中,显示可观察集合中的每个员工现在我想对可观察集合进行排序,以便我的应用程序的UI将根据可观察集合进行排序而改变……谢谢]如果您可以接受答案,那就太好了……一个排序的可观察集合的替代实现:.Codeplex链接已断开请注意,PropertyName
不能使用绑定。它直接导致以下运行时错误:无法在“SortDescription”类型的“PropertyName”属性上设置“Binding”。“绑定”只能在DependencyObject的DependencyProperty上设置。
@OndrejJanacek这是一种新行为吗?我可以发誓我就是这样用的。但是我再也不能访问代码来检查它了。@好吧,你这样使用它的可能性不高。该属性毕竟被称为PropertyName
,这表明它可以采用属性的字符串名称,而不是直接绑定到属性。但我是WPF的新手,我只是偶然发现了这一点,因为我在寻找解决方案并实现了它,所以它可能以前以另一种方式工作。@OndrejJanacek无论如何。。。谢谢你的编辑。我该怎么做?
ItemsSource="{Binding Source={StaticResource ItemListViewSource}}"