C# 从列表中删除时,以随机顺序列出项目记录
我有列表视图表。每当我试图从列表视图表中删除该项时,列表的顺序就会变得随机,但当我向列表中添加一项时,列表就会被排序。无论何时删除列表,我都希望其余的项目按学生ID排序。我已经尝试过了,到目前为止没有任何运气 StudentOrder.xamlC# 从列表中删除时,以随机顺序列出项目记录,c#,wpf,xaml,C#,Wpf,Xaml,我有列表视图表。每当我试图从列表视图表中删除该项时,列表的顺序就会变得随机,但当我向列表中添加一项时,列表就会被排序。无论何时删除列表,我都希望其余的项目按学生ID排序。我已经尝试过了,到目前为止没有任何运气 StudentOrder.xaml <CollectionViewSource Source="{Binding StudentRecords}"> <CollectionViewSource.SortDescriptions> </C
<CollectionViewSource
Source="{Binding StudentRecords}">
<CollectionViewSource.SortDescriptions>
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
ShowOrders.cs
public override bool Delete()
{
recordRetriever.StudentOrderRetriever.Delete(_record);
Record = null;
Refresh();
return true;
}
private ObservableCollection<ShowOrder> RandomListSort()
{
IEnumerable<ShowOrder> arrangeditems = StudentRecords;
if (Orderreplaced)
{
switch (DataOrder)
{
default:
arrangeditems = StudentRecords.OrderBy(s => s.GetType().GetProperty(DataOrder).GetValue(s, null)).ToList();
break;
}
}
Orderreplaced = false;
return new ObservableCollection<RandomListSort>(arrangeditems);
}
public override void Refresh()
{
Refresh();
StudentRecords = RandomListSort();
}
public override bool Delete()
{
recordRetriever.StudentOrderRetriever.Delete(_记录);
记录=null;
刷新();
返回true;
}
私有ObservableCollection RandomListSort()
{
IEnumerable arrangeditems=StudentRecords;
如果(已替换订单)
{
开关(数据顺序)
{
违约:
arrangeditems=StudentRecords.OrderBy(s=>s.GetType().GetProperty(DataOrder).GetValue(s,null)).ToList();
打破
}
}
Orderreplaced=false;
返回新的ObservableCollection(arrangeditems);
}
公共覆盖无效刷新()
{
刷新();
StudentRecords=RandomListSort();
}
没有为排序描述设置属性。如果不是这样,最好在XAML中设置它,如下所示
<CollectionViewSource
Source="{Binding StudentRecords}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="your Propery from bound source" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
有关更多文档,请查看下面的链接
问题是什么?为什么要在
Refresh()
中调用Refresh()
?还有什么是Show
,它是否与ShowOrders
相关?如果是,您是否已在RandomListSort
中对学生记录进行排序您是否删除了自己尝试的任何其他排序代码。如果没有,就把它取下来试试。是的,什么都试过了,但都不管用,所以我来问你。
<CollectionViewSource
Source="{Binding StudentRecords}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="your Propery from bound source" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>