wpf ListBox拖动排序实现
来源:互联网 发布:淘宝美工面试 编辑:程序博客网 时间:2024/05/17 03:57
XAML:
<Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ListBox Name="LBoxSort" PreviewMouseMove="LBoxSort_OnPreviewMouseMove" Drop="LBoxSort_OnDrop" AllowDrop="True"> <TextBox Text="1111"/> <TextBlock Text="2222"/> <TextBlock Text="3333"/> <TextBlock Text="4444"/> <TextBlock Text="5555"/> </ListBox> </Grid></Window>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApplication6{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void LBoxSort_OnPreviewMouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { var pos = e.GetPosition(LBoxSort); HitTestResult result = VisualTreeHelper.HitTest(LBoxSort, pos); if (result == null) { return; } var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit); if (listBoxItem == null || listBoxItem.Content != LBoxSort.SelectedItem) { return; } DataObject dataObj = new DataObject(listBoxItem.Content as TextBlock); DragDrop.DoDragDrop(LBoxSort, dataObj, DragDropEffects.Move); } } private void LBoxSort_OnDrop(object sender, DragEventArgs e) { var pos = e.GetPosition(LBoxSort); var result = VisualTreeHelper.HitTest(LBoxSort, pos); if (result == null) { return; } //查找元数据 var sourcePerson = e.Data.GetData(typeof(TextBlock)) as TextBlock; if (sourcePerson == null) { return; } //查找目标数据 var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit); if (listBoxItem == null) { return; } var targetPerson = listBoxItem.Content as TextBlock; if (ReferenceEquals(targetPerson, sourcePerson)) { return; } LBoxSort.Items.Remove(sourcePerson); LBoxSort.Items.Insert(LBoxSort.Items.IndexOf(targetPerson), sourcePerson); } } internal static class Utils { //根据子元素查找父元素 public static T FindVisualParent<T>(DependencyObject obj) where T : class { while (obj != null) { if (obj is T) return obj as T; obj = VisualTreeHelper.GetParent(obj); } return null; } }}
0 0
- wpf ListBox拖动排序实现
- WPF-ListBox分组-排序
- 用C#实现在ListBox中拖动排序
- 用C#实现在ListBox中拖动排序
- WPF实现控件拖动
- WPF Popup实现拖动
- Silverlight 4.0 可拖动排序的ListBox
- VB.Net程序设计:ListBox拖动排序两个ListBox的操作
- .net C# listbox的上下移动,拖动排序,两个listbox相互拖动
- WPF 自定义listbox的ItemTemplate实现通讯录
- WPF ListBox
- WPF ListBox
- WPF ListBox
- 在winform的listbox中进行拖动排序
- jquery实现拖动排序
- GridView实现拖动排序
- WPF下实现控件的拖动操作
- c# wpf 实现拖动图片功能
- C++中不能声明为虚函数的有哪些函数
- MyEclipse 2014 破解图文详细教程【转】
- Hello World
- BaseColumns类的作用
- MATLAB谢菲尔德遗传算法工具箱使用
- wpf ListBox拖动排序实现
- android studio配置retrolambda
- JNI介绍
- 创建对象
- NKOI 1015(USCAO 2.2.2)子集
- Gvim开发环境配置笔记--Windows篇
- 飞机加油问题
- C++作业3
- Codeforces 630I Parking Lot