wpf - 如何在 WPF ItemsControl 中保留项目的排序顺序?

标签 wpf data-binding sorting drag-and-drop listbox

我的应用程序中有一个可排序的列表框,其中包含一系列具有 DisplayOrder 属性的项目。这些项目最初按 DisplayOrder 排序,但用户可以通过拖放对其重新排序。

我通过使用 SortDescription 将 ListBox 的 ItemsSource 设置为 CollectionViewSource 来设置初始排序顺序。

发生拖放操作后,如何更新受影响项的 DisplayOrder 属性?

这是我在放置操作的事件处理程序中做的事情,还是有一种方法可以将 ListBox 的索引绑定(bind)到我的 DisplayOrder 属性?

最佳答案

我在 xaml 文件的代码后面处理了这个。我将事件操作传递到我的 View 模型,然后在代码中执行操作并让 INotify 使用新顺序更新 UI。

查看

ChangedEvent (button click / drag drop / whatever)
{
    ViewModel.MoveItemToNewLocation();

}

查看模型

MoveItemToNewLocation()
{
   int newLocation = myList.IndexOf(SelectedItem); 
   int oldLocation = SelectedItem.DisplayOrder; 
   UpdateDisplayOrders(oldLocation, newLocation);
}

private void UpdateDisplayOrders(int oldlocation, int newlocation, object myitem)
{
   // Do move logic here

}

关于wpf - 如何在 WPF ItemsControl 中保留项目的排序顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6559631/

相关文章:

.net - ItemsControl 绑定(bind)当前对象?

c# - 基本理解: binding methods with arguments to controls

c# - 如何使用 Xamarin Form 以编程方式在现有网格上添加按钮或标签?

c# - 有没有办法全局更改 wpf 中绑定(bind)的默认行为?

data-binding - Grails 将请求参数绑定(bind)到枚举

python - 带有字母 'x' 关键参数的排序列表

android - RuleBasedCollat​​or 不适用于 Android?

Java:使用外部参数对数组进行排序

c# - WPF 自定义数据网格列标题

WPF - 如何设置菜单控件的样式以删除左边距?