[英]How can I sort an ObservableCollection<T> that's bound to a ListBoxControl?
I've got a winform app that uses 2 DevExpress ListBoxControls that a user can move items from one to the other. 我有一个Winform应用程序,它使用2个DevExpress ListBoxControls,用户可以将项目从一个移动到另一个。 The box on the left has "All available" Sections while the box on the right has what's currently assigned to the user. 左侧的框具有“所有可用”部分,而右侧的框具有当前分配给用户的内容。 I am new to Generics but am trying to implement them in this project. 我是泛型新手,但正在尝试在此项目中实现它们。 Each ListBoxControl is bound to an ObservableCollection via it's DataSource: 每个ListBoxControl都通过其DataSource绑定到ObservableCollection:
ObservableCollection<Sections> allSections = new ObservableCollection<Sections>((dbContext.Sections
.OrderBy(s => s.SectionName).AsEnumerable<Sections>()));
listboxAllSections.DataSource = allSections;
listboxAllSections.DisplayMember = "SectionName";
listboxAllSections.ValueMember = "SectionID";
So I have 4 buttons in between each listbox to allow the user to move items back and forth: 因此,我在每个列表框之间有4个按钮,以允许用户来回移动项目:
MoveRight >
向右移动>
MoveAllRight >>
MoveAllRight >>
MoveLeft <
MoveLeft <
MoveAllLeft <<
MoveAllLeft <<
For MoveRight
and MoveLeft
I created this Generic function: 对于MoveRight
和MoveLeft
我创建了以下泛型函数:
private void MoveItem<T>(ListBoxControl source, ListBoxControl target)
{
ObservableCollection<T> sourceItems = (ObservableCollection<T>)source.DataSource;
ObservableCollection<T> targetItems = (ObservableCollection<T>)target.DataSource;
target.BeginUpdate();
//Add items to target.
foreach (T item in source.SelectedItems)
targetItems.Add(item);
//Remove items from source.
for (int x = 0; x < source.SelectedItems.Count; x++)
sourceItems.Remove((T)source.SelectedItems[x]);
target.EndUpdate();
}
All works great, but I would like to add sorting to it. 一切都很好,但我想对其进行排序。 I don't need sorting when moving items to the right, only when moving items to the left. 我不需要在向右移动项目时进行排序,只需在向左移动项目时进行排序。 I can't figure out how to sort a generic collection based on a certain property. 我不知道如何基于某个属性对通用集合进行排序。 For instance, Sections
has a property called SectionName
that I'd like to sort on. 例如, Sections
有一个名为SectionName
的属性,我想对其进行排序。 I can do this if I use the actual type: 如果使用实际类型,则可以执行以下操作:
ObservableCollection<Sections> sourceItems = (ObservableCollection<Sections>)listboxAllSections.DataSource;
var sortedItems = new ObservableCollection<Sections>();
foreach (var item in sourceItems.OrderBy(t => t.SectionName))
sortedItems.Add(item);
listboxAllSections.DataSource = sortedItems;
But I don't know how to make this generic to that sourceItems.OrderBy(t => t.SectionName)
can be the field of the Type that's passed in. 但是我不知道如何使它对那个sourceItems.OrderBy(t => t.SectionName)
通用,可以是传入的Type的字段。
Any help/guidance is appreciated! 任何帮助/指导表示赞赏!
you could do this: 您可以这样做:
private void MoveItem<T>(ListBoxControl source, ListBoxControl target)
where T : IComparable
{
var sortedItems = new ObservableCollection<T>();
var sordet = sourceItems.OrderBy(t => t);
}
if T
knows how to compare it self against anything, you can call List<T>.OrderBy(c => c)
(<-- pseudo code) and you will always have a sorted list. 如果T
知道如何将自身与任何事物进行比较,则可以调用List<T>.OrderBy(c => c)
(<-伪代码),并且始终会有一个排序列表。
I guess you should try using a CollectionViewSource
to sort data in your ListBox
. 我猜您应该尝试使用CollectionViewSource
对ListBox
数据进行排序。 This way you do not have to keep the data sorted in the ObservableCollection
. 这样,您不必将数据保留在ObservableCollection
。 Besides, it separates your code-behind / ViewModel from the presentation logic. 此外,它会将您的代码隐藏/ ViewModel与表示逻辑分离。 There is an example showing how to use a CollectionViewSource on MSDN . 有一个示例显示了如何在MSDN上使用CollectionViewSource 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.