wpf listBox 拖拽排序

<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"/>
            <TextBlock Text="2222"/>
            <TextBlock Text="3333"/>
            <TextBlock Text="4444"/>
            <TextBlock Text="5555"/>
            <TextBlock Text="2222"/>
            <TextBlock Text="3333"/>
            <TextBlock Text="4444"/>
            <TextBlock Text="5555"/>
            <TextBlock Text="2222"/>
            <TextBlock Text="3333"/>
            <TextBlock Text="4444"/>
            <TextBlock Text="5555"/>
            <TextBlock Text="2222"/>
            <TextBlock Text="3333"/>
            <TextBlock Text="4444"/>
            <TextBlock Text="5555"/>
        </ListBox>
    </Grid>

 

 public partial class ListBoxIsDrag : UserControl
    {
        public ListBoxIsDrag()
        {
            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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值