【桌面开发】WPF ListView/ListBox的item项拖拽实现

文章目录

    • 一、前言
    • 二、代码
    • 三、运行情况
    • 四、小结

一、前言

今天项目中listview的item项要实现拖拽上下移动,写好了,给出demo,listbox也是一样的,毕竟listbox是listview的父类,同一类东西。

二、代码

xaml代码

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListView Name="LBoxSort" PreviewMouseMove="LBoxSort_OnPreviewMouseMove" Drop="LBoxSort_OnDrop" AllowDrop="True">
            <TextBlock Text="1111"/>
            <TextBlock Text="2222"/>
            <TextBlock Text="3333"/>
            <TextBlock Text="4444"/>
            <TextBlock Text="5555"/>
        </ListView>
    </Grid>
</Window>

cs代码

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);  // 获取位置 

                #region 源位置
                HitTestResult result = VisualTreeHelper.HitTest(LBoxSort, pos);  //根据位置得到result
                if (result == null)
                {
                    return;    //找不到 返回
                }
                var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
                if (listBoxItem == null || listBoxItem.Content != LBoxSort.SelectedItem)
                {
                    return;
                }
                #endregion

                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);   //根据位置得到result
            if (result == null)
            {
                return;   //找不到 返回
            }
            #region 查找元数据
            var sourcePerson = e.Data.GetData(typeof(TextBlock)) as TextBlock;
            if (sourcePerson == null)
            {
                return;
            }
            #endregion

            #region  查找目标数据
            var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
            if (listBoxItem == null)
            {
                return;
            }
            var targetPerson = listBoxItem.Content as TextBlock;
            if (ReferenceEquals(targetPerson, sourcePerson))
            {
                return;
            }
            #endregion

           
            int sourceIndex = LBoxSort.Items.IndexOf(sourcePerson);
            int targetIndex = LBoxSort.Items.IndexOf(targetPerson);

            if(sourceIndex < targetIndex)  //从上面移动到下面
            {
                LBoxSort.Items.Remove(sourcePerson);  //删除源 
                Console.WriteLine(LBoxSort.Items.IndexOf(targetPerson) + 1);
                LBoxSort.Items.Insert(LBoxSort.Items.IndexOf(targetPerson) + 1, sourcePerson);
            }
            else if (sourceIndex>targetIndex)
            {
                LBoxSort.Items.Remove(sourcePerson);  //删除源 
                Console.WriteLine(LBoxSort.Items.IndexOf(targetPerson) );
                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;
        }
    }

三、运行情况

【桌面开发】WPF ListView/ListBox的item项拖拽实现_第1张图片

四、小结

listview的item项实现拖拽上下移动,写好了,天天打码,天天进步!!!

源代码地址:https://download.csdn.net/download/qq_36963950/12496956

你可能感兴趣的:(桌面开发(C/C++,C#))