wpf ListBox拖动排序实现

XAML:

[html]  view plain  copy
  1. <Window x:Class="WpfApplication6.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.     <Grid>  
  6.         <ListBox Name="LBoxSort" PreviewMouseMove="LBoxSort_OnPreviewMouseMove" Drop="LBoxSort_OnDrop" AllowDrop="True">  
  7.             <TextBox Text="1111"/>  
  8.             <TextBlock Text="2222"/>  
  9.             <TextBlock Text="3333"/>  
  10.             <TextBlock Text="4444"/>  
  11.             <TextBlock Text="5555"/>  
  12.         </ListBox>  
  13.     </Grid>  
  14. </Window>  

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14.   
  15. namespace WpfApplication6  
  16. {  
  17.     /// <summary>  
  18.     /// MainWindow.xaml 的交互逻辑  
  19.     /// </summary>  
  20.     public partial class MainWindow : Window  
  21.     {  
  22.         public MainWindow()  
  23.         {  
  24.             InitializeComponent();  
  25.         }  
  26.         private void LBoxSort_OnPreviewMouseMove(object sender, MouseEventArgs e)  
  27.         {  
  28.             if (e.LeftButton == MouseButtonState.Pressed)  
  29.             {  
  30.                 var pos = e.GetPosition(LBoxSort);  
  31.                 HitTestResult result = VisualTreeHelper.HitTest(LBoxSort, pos);  
  32.                 if (result == null)  
  33.                 {  
  34.                     return;  
  35.                 }  
  36.                 var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);  
  37.                 if (listBoxItem == null || listBoxItem.Content != LBoxSort.SelectedItem)  
  38.                 {  
  39.                     return;  
  40.                 }  
  41.                 DataObject dataObj = new DataObject(listBoxItem.Content as TextBlock);  
  42.                 DragDrop.DoDragDrop(LBoxSort, dataObj, DragDropEffects.Move);  
  43.             }  
  44.         }  
  45.   
  46.         private void LBoxSort_OnDrop(object sender, DragEventArgs e)  
  47.         {  
  48.             var pos = e.GetPosition(LBoxSort);  
  49.             var result = VisualTreeHelper.HitTest(LBoxSort, pos);  
  50.             if (result == null)  
  51.             {  
  52.                 return;  
  53.             }  
  54.             //查找元数据  
  55.             var sourcePerson = e.Data.GetData(typeof(TextBlock)) as TextBlock;  
  56.             if (sourcePerson == null)  
  57.             {  
  58.                 return;  
  59.             }  
  60.             //查找目标数据  
  61.             var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);  
  62.             if (listBoxItem == null)  
  63.             {  
  64.                 return;  
  65.             }  
  66.             var targetPerson = listBoxItem.Content as TextBlock;  
  67.             if (ReferenceEquals(targetPerson, sourcePerson))  
  68.             {  
  69.                 return;  
  70.             }  
  71.             LBoxSort.Items.Remove(sourcePerson);  
  72.             LBoxSort.Items.Insert(LBoxSort.Items.IndexOf(targetPerson), sourcePerson);  
  73.         }  
  74.     }  
  75.     internal static class Utils  
  76.     {  
  77.         //根据子元素查找父元素  
  78.         public static T FindVisualParent<T>(DependencyObject obj) where T : class  
  79.         {  
  80.             while (obj != null)  
  81.             {  
  82.                 if (obj is T)  
  83.                     return obj as T;  
  84.   
  85.                 obj = VisualTreeHelper.GetParent(obj);  
  86.             }  
  87.             return null;  
  88.         }  
  89.     }  
  90. }  
实现ListBox拖动排序,需要使用WPF和MVVM的一些技巧。下面是一些大致的步骤: 1. 在ViewModel中定义一个ObservableCollection,用于绑定ListBox的ItemsSource属性。 2. 在ListBox的ItemTemplate中,绑定ListBoxItem的PreviewMouseDown事件和PreviewMouseMove事件。这两个事件分别用于开始拖动和移动拖动项。 3. 在PreviewMouseDown事件中,使用VisualTreeHelper找到ListBoxItem的父ListBox,并将拖动项从ObservableCollection中移除。 4. 在PreviewMouseMove事件中,使用DragDrop.DoDragDrop方法启动拖动操作,并将拖动项的索引和DataObject传递给该方法。 5. 在ViewModel中,定义一个DragOver命令和一个Drop命令。DragOver命令用于处理拖动项经过其他项时的逻辑,Drop命令用于处理拖动项放下时的逻辑。这两个命令的参数都包含了拖动项的索引和目标项的索引。 6. 在ListBox的ItemContainerStyle中,绑定ListBoxItem的DragOver事件和Drop事件到ViewModel中的DragOver命令和Drop命令。 7. 在ViewModel的Drop命令中,将拖动项插入到ObservableCollection中目标项的位置,并更新所有项的排序。 这些步骤只是大致的概述,具体实现时还需要考虑一些细节,例如拖动项和其他项的样式、拖动项和其他项的交互效果等等。如果您需要更详细的实现细节,请参考以下链接: https://www.c-sharpcorner.com/UploadFile/raj1979/drag-and-drop-items-in-listbox-in-wpf-mvvm/ https://www.codeproject.com/Articles/30905/Drag-and-Drop-Items-in-WPF-ListView-Control https://www.wpf-tutorial.com/listview-control/listview-drag-drop-sorting-mvvm/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值