简体   繁体   English

我如何排序一个ObservableCollection <T> 那绑定到一个ListBoxControl吗?

[英]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: 对于MoveRightMoveLeft我创建了以下泛型函数:

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 . 我猜您应该尝试使用CollectionViewSourceListBox数据进行排序。 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.

相关问题 我怎样才能对ObservableCollection进行排序 <T> 这样我的UI也会看到排序过程? - How can I sort ObservableCollection<T> such that my UI also sees the process of sorting? 在 MVVM 中对绑定到 DataGrid 的 ObservableCollection 进行排序 - Sort ObservableCollection bound to DataGrid in MVVM 更新绑定到它的ObservableCollection后,DataGrid不会更新 - DataGrid doesn't get updated after updating ObservableCollection that's bound to it 如何在ObservableCollection之后刷新绑定的GridView <T> 已排序? - How do I refresh a bound GridView after the ObservableCollection<T> has be sorted? 如何为T添加ObservableCollection <Class <T >>使用继承 - How can I add an ObservableCollection<Class<T>> use inheritance for T 绑定到 ObservableCollection (WPF) 时无法更改 ComboBox 选择 - Can't change ComboBox selection when bound to ObservableCollection (WPF) 我如何检查ObservableCollection的“ CollectionChanged”事件是否为null - how can i check ObservableCollection's “CollectionChanged” event is null or not 对 ObservableCollection 排序<t>按类型</t> - Sort ObservableCollection<T> by type 如何测试值是否是 ObservableCollection 的实例<T> ? - How can I test if a value is an instance of ObservableCollection<T>? 如何在ObservableCollection上执行CanDelete验证 <T> ? - How can I do CanDelete validation on ObservableCollection<T>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM