C# 这实际上是如何对列表中的项目重新排序的?
我正在寻找一种使用MVVM对wpf列表中的项目重新排序的方法。我发现这两个代码示例看起来很神奇:C# 这实际上是如何对列表中的项目重新排序的?,c#,wpf,mvvm,C#,Wpf,Mvvm,我正在寻找一种使用MVVM对wpf列表中的项目重新排序的方法。我发现这两个代码示例看起来很神奇: 及其 我研究了代码,了解了它是如何使用itemsreorderedent通知想要处理它的人,ListBox中的项目已被重新排序的 我只是不明白它是如何重新订购的。。事实上,我已经在一个简单的应用程序中尝试了该代码,即使我处理了该事件,UI中的任何内容实际上都不会在集合中移动 我假设我需要在PreviewMouseLeftButtonup的中添加代码。使用原始项、其索引和新索引 既然代码已经全部
- 及其
itemsreorderedent
通知想要处理它的人,ListBox
中的项目已被重新排序的
我只是不明白它是如何重新订购的。。事实上,我已经在一个简单的应用程序中尝试了该代码,即使我处理了该事件,UI中的任何内容实际上都不会在集合中移动
我假设我需要在PreviewMouseLeftButtonup的中添加代码。使用原始项
、其索引和新索引
既然代码已经全部编写好了,我想知道为什么我需要完成它。如果我错了,它实际上不需要我提供更多的代码,那么我缺少什么呢
以下是我的简单应用程序的代码:
public partial class MainWindow : Window
{
public ObservableCollection<String> list { get; set; }
public MainWindow()
{
DataContext = this;
list = new ObservableCollection<string>();
list.Add("one");
list.Add("two");
list.Add("three");
InitializeComponent();
}
private void ReorderableListBox_ItemsReordered(object sender, RoutedEventArgs e)
{
Console.WriteLine("sdags");
}
}
公共部分类主窗口:窗口
{
公共ObservableCollection列表{get;set;}
公共主窗口()
{
DataContext=this;
列表=新的ObservableCollection();
列表。添加(“一”);
列表。添加(“两个”);
列表。添加(“三”);
初始化组件();
}
private void ReorderableListBox_ItemsReordered(对象发送方,RoutedEventArgs e)
{
控制台写入线(“sdags”);
}
}
以及xaml:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ReorderableListBoxProject_XavText" x:Class="ReorderableListBoxProject_XavText.MainWindow"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:ReorderableListBox ItemsSource="{Binding list}" ItemsReordered="ReorderableListBox_ItemsReordered"/>
</Grid>
编辑
这是:
可能是因为它希望您手动操作
不久前,我也需要同样的东西,最后写了一篇小文章
它没有口哨和铃铛,但它完成了它的工作
using System.Windows.Controls;
using System.Windows.Input;
namespace ReorderableListBox
{
public class ReorderableListBox : ListBox
{
private bool _canDrag;
private int _sourceIndex;
private int _targetIndex;
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
if (Items == null || SelectedItem == null) return;
if (_canDrag)
_sourceIndex = Items.IndexOf(SelectedItem);
if (_targetIndex == -1 || _sourceIndex == -1 || !_canDrag) return;
var item1 = (ListBoxItem)Items[_targetIndex];
var item2 = (ListBoxItem)Items[_sourceIndex];
object content1 = item1.Content;
object content2 = item2.Content;
item1.Content = content2;
item2.Content = content1;
_canDrag = false;
}
protected override void OnPreviewMouseMove(MouseEventArgs e)
{
base.OnPreviewMouseMove(e);
var canDrag = e.LeftButton == MouseButtonState.Pressed;
if (canDrag && Items != null && SelectedItem != null)
_targetIndex = Items.IndexOf(SelectedItem);
_canDrag = canDrag;
}
}
}
正如您所见,它非常简单但有效,可以随意添加装饰、预览等
编辑
您需要将项目包装在ListBoxItem中
IEnumerable<int> enumerable = Enumerable.Range(0, 10);
foreach (int i in enumerable)
{
ListBox1.Items.Add(new ListBoxItem {Content = i});
}
IEnumerable enumerable=可枚举范围(0,10);
foreach(可枚举中的int i)
{
添加(新的ListBoxItem{Content=i});
}
我试图通过删除强制转换来升级代码,以允许任何类型的对象,不幸的是,它不能可靠地工作,需要更多的时间来修复它。(我没有时间)我试过你的代码,但它对我不起作用。我无法将任何内容(从字符串到自定义对象)强制转换为ListBoxItem。它给出了一个“'System.InvalidCastException'”,它对任何类型的对象都有效,发布你无法使用的相关代码,我来看看。