在不破坏绑定的情况下对ObservableCollection进行排序 [英] Sort ObservableCollection without destroying bindings

查看:85
本文介绍了在不破坏绑定的情况下对ObservableCollection进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,这里的设置太长了...

Sorry for the long setup here...

我在WPF中有一个错误和增强跟踪应用程序.可以输入错误,然后用固定的版本标记.固定版本包含两个部分,主要版本(例如2.0版)和次要版本(例如12.16.11版).

I have a bug and enhancement tracking application in WPF.  Bugs can be entered and then marked with a fixed version.  Fixed versions contain two parts, a major version (e.x. 2.0) and a minor version (e.x. 12.16.11).

所以,我有一个SoftwareClass.  此类包含ObservableCollection< MajorVersionClass>的属性.   MajorVersionClass包含ObservableCollection< MinorVersionClass>的属性.希望到目前为止,这是有道理的.  将有一个ObservableCollection< SoftwareClass>它将存储我的组织生产的每个软件产品.  每个软件项目都将包含THAT软件项目的主要版本的ObservableCollection,并且每个MajorVersion将 包含次要版本的ObservableCollection.

So, I have a SoftwareClass.  This class contains a property of ObservableCollection<MajorVersionClass>.  The MajorVersionClass contains a property of ObservableCollection<MinorVersionClass>.  Hopefully this makes sense so far.  There will be an ObservableCollection<SoftwareClass> which will store each software product my organization makes.  Each software item will contain the ObservableCollection of major versions for THAT software item, and each MajorVersion will contain an ObservableCollection of minor versions.

最后,当然有情境类",其中包含Software,MajorVersion和MinorVersion的属性.

Lastly, there's of course the SituationClass which contains properties for the Software, MajorVersion and MinorVersion.

现在,我有一个售票窗口,用户可以在其中写问题或请求.  有一个组合框,其中ItemsSource绑定到了我的ObservableCollection< SoftwareClass>.  接下来,有一个ComboBox,其ItemsSource绑定到SoftwareComboBox.SelectedItem.MajorVersions -也就是说,它绑定到所选的任何软件以及其中的主要版本.  这些ComboBox控件的SelectedItem绑定到现状类的实例和相应的属性.

Now, I have a ticket window where users can write a problem or request.  There's a ComboBox with ItemsSource bound to my ObservableCollection<SoftwareClass>.  Next, there's a ComboBox with ItemsSource bound to SoftwareComboBox.SelectedItem.MajorVersions -- That is, it's bound to whatever software is selected, and to the major versions in it.  The SelectedItem of these ComboBox controls is bound to an instance of SituationClass and the corresponding properties.

最后,这就是问题所在.在用户添加新版本之前,一切正常.  列表不会重复使用,因为版本会添加到列表的末尾.我添加了一个静态方法来对ObservableCollection进行排序:

Finally, here's the problem.  All is working until a user adds a new version.  The lists do not get resorted, as the version adds at the end of the list.  I added a static method to sort ObservableCollection:

 

static class Extensions
   {
      public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable
      {
         // Order by and put into a list.
         List<T> sorted = collection.OrderBy(x => x).ToList();

         // Loop the list and exchange items in the collection.
         for (int i = sorted.Count() - 1; i >= 0; i--)
         {
            collection.Insert(0, sorted[i]);
            collection.RemoveAt(collection.Count - 1);
         }
      }
   }

当删除并添加收集项时,窗口上的ComboBox变得混乱.  删除该项目后,组合框将清除.

When the collection item is removed and added, the ComboBox on the windows gets messed up.  When the item is removed, the combobox clears.

是否有某种方法可以对ObservableCollection进行排序而又不会丢失绑定中的所选项目?  我应该使用另一个类吗?

Is there some way to sort the ObservableCollection without losing my selected item in the binding?  Is there another class I should be using?

推荐答案

我相信您正在寻找ObservableCollection.

I believe you are looking for ObservableCollection.Move.  This allows you to move an item within the collection without removing and readding it.

 


这篇关于在不破坏绑定的情况下对ObservableCollection进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆