对集合进行永久排序

发布于 2024-11-29 09:50:02 字数 539 浏览 4 评论 0原文

我有一个可观察的集合,它通过 collectionviewsource 向用户公开。集合中项目的属性之一是排序顺序。我试图允许用户永久使用此集合并将更改传播到数据库。

我有 CVS 工作,我可以在其中显示列表框中显示的各个项目。然而现在我必须更改 item.sortorder==cvs.currentindex 并且我无法找出执行此操作的正确方法。

编辑

显然我还不够清楚。 Sortorder 是我的数据库中的一个字段,它是我的对象的一部分,允许用户控制列表控件中显示的项目的位置。我试图让我的用户能够通过将 sortorder 字段的值更改为等于显示项目的当前索引来更改这些项目将来的排序方式。

items 当前的 sortorder 值为 3。

用户将显示的 listitem 移动到位置 0(即第一个位置)

items 新的 sortorder=0 原始 sortorder 的 item 将变为 1 等等,

这可以通过循环排序的 CVS 并使 Item.SortOrder= CVS 来实现。项目索引

I have an observable collection that is exposed to the user by a collectionviewsource. One of the properties on the items in the collection is sortorder. I am trying to allow the user to permanently resort this collection and propagate the changes to the db.

I have the CVS working where I can resort the individual items as they are displayed in the listbox. Now however I have to change the item.sortorder==cvs.currentindex and i am having trouble figuring out the proper way to do this.

EDIT

evidently I was not clear enough. Sortorder is a field in my DB that is part of my object that allows the user to control position of the items as displayed in list controls. I am trying to give my user the ability to change how these items are sorted in the future by changing the value of the sortorder field to equal the current index of the displayed item.

items current sortorder value is 3.

user moves displayed listitem to position 0(ie first position)

items new sortorder=0 item with original sortorder will become 1 etc

this would be achieved by looping through the sorted CVS and making Item.SortOrder= CVS.Item.index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你另情深 2024-12-06 09:50:02

数据库通常根据添加到表中的顺序从表中返回行,除非您在查询中指定 order by 子句。更改数据库中的顺序不是一个好主意。相反,尝试使用参数化查询并传入您希望排序的列和方向(从用户首选项中检索)。

Databases return rows from table based typically on the order they were added to the table unless you specify an order by clause in your query. changing the order in the database is not a good idea. instead, try something such as using a parameterized query and passing in the column and direction you wish to be sorted which is retrieved from a user preference.

梦亿 2024-12-06 09:50:02

我想通了。

我回去查看了用来更改元素位置的代码,实际上我正在使用集合本身:

private void OnDecreaseSortOrderCommandExecute()
        {
            int index = QuestionsCVS.View.CurrentPosition;
            QuestionsViewModel item = SelectedQuestionVM;

            if (item != null && index > 0)
            {
                SortableQuestionsVMCollection.RemoveAt(index);
                SortableQuestionsVMCollection.Insert(index - 1, item);
                QuestionsCVS.View.Refresh();
                QuestionsCVS.View.MoveCurrentTo(item);
            }
        }

所以我只是这样做了:

private void ResortSortableQuestionsVMCollection()
        {
            for (int i = 0; i < SortableQuestionsVMCollection.Count; i++)
            {
                SortableQuestionsVMCollection[i].SortOrder = i;
            }
        }

我不知道这是否适用于所有情况,但它确实做了我想要的事情想要这个。

I figured it out.

I went back and looked at the code that I was using to change the position of the elements and I am actually using the collection itself:

private void OnDecreaseSortOrderCommandExecute()
        {
            int index = QuestionsCVS.View.CurrentPosition;
            QuestionsViewModel item = SelectedQuestionVM;

            if (item != null && index > 0)
            {
                SortableQuestionsVMCollection.RemoveAt(index);
                SortableQuestionsVMCollection.Insert(index - 1, item);
                QuestionsCVS.View.Refresh();
                QuestionsCVS.View.MoveCurrentTo(item);
            }
        }

So I simply did this:

private void ResortSortableQuestionsVMCollection()
        {
            for (int i = 0; i < SortableQuestionsVMCollection.Count; i++)
            {
                SortableQuestionsVMCollection[i].SortOrder = i;
            }
        }

I dont know if this will work in every circumstance but it certainly did what I wanted for this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文