zoukankan      html  css  js  c++  java
  • wpf ListBox拖动排序实现(转载)

    <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;
            }
        }
    }

    转载于:https://blog.csdn.net/linraise/article/details/51107252#

  • 相关阅读:
    Python面试题汇总
    异常处理及反射
    DOM
    socket
    python生成器、迭代器、列表 字典 集合解析、迭代器表达式
    C++繁琐的类型转换,C++小知识之四种类型转换
    C++命名空间
    C++合并exe文件,将两个合并成一个
    BERT 之后,Transformer 还会出现高光时刻吗
    App开发常见布局,流式布局讲解
  • 原文地址:https://www.cnblogs.com/MarcLiu/p/14441542.html
Copyright © 2011-2022 走看看