zoukankan      html  css  js  c++  java
  • ListBox实现拖拽排序功能

    1、拖拽需要实现的事件包括:

    PreviewMouseLeftButtonDown

    LBoxSort_OnDrop

    具体实现如下:

     private void LBoxSort_OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                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 Person);
                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 (Person)) as Person;
                if (sourcePerson == null)
                {
                    return;
                }
                //查找目标数据
                var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
                if (listBoxItem == null)
                {
                    return;
                }
                var targetPerson = listBoxItem.Content as Person;
                if (ReferenceEquals(targetPerson, sourcePerson))
                {
                    return;
                }
                _persons.Remove(sourcePerson);
                _persons.Insert(_persons.IndexOf(targetPerson), sourcePerson);
            }
        }
    事件实现

    2、排序功能实现:
    数据源:

    private ObservableCollection<Person> _persons = new ObservableCollection<Person>();
     private void InitData()
            {
                _persons.Add(new Person{Name = "test1", Order = "1"});
                _persons.Add(new Person { Name = "test2", Order = "2" });
                _persons.Add(new Person { Name = "test3", Order = "3" });
                _persons.Add(new Person { Name = "test4", Order = "4" });
                _persons.Add(new Person { Name = "test5", Order = "5" });
            }
    数据源

    3、排序功能实现:
    为数据集合实现CollectionChanged事件,当数据集合发生变化时执行:

      private void PersonsOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    for (int i = e.OldStartingIndex; i < _persons.Count; i++)
                    {
                        _persons[i].Order = i.ToString();
                    }
                }
                else if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    for (int i = e.NewStartingIndex; i < _persons.Count; i++)
                    {
                        _persons[i].Order = i.ToString();
                    }
                }
            }
    排序

    4、相关代码附加:

     public class Person : INotifyPropertyChanged
        {
            public string Name { get; set; }
    
            private string _order;
            public string Order
            {
                get { return _order; }
                set { _order = value; OnPropertyChanged("Order"); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
          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;
             }
         }
    相关代码

     5、代码下载地址:

    http://download.csdn.net/detail/w_wanglei/6375371

  • 相关阅读:
    PowerShell_零基础自学课程_7_Powershell中重定向机制、目录和文件管理
    volcanol_C语言学习趣事汇总贴
    linux_shell_2_shell 中的变量特性
    PowerShell_零基础自学课程_5_自定义PowerShell环境及Powershell中的基本概念
    volcanol_Linux_问题汇总系列_2_如何在linux和windows主机之间共享文件
    C语言学习趣事_你不知道的C语言应用
    volcanol_Windows_Powershell_汇总贴
    PowerShell_零基础自学课程_4_PowerShell的别名功能、错误管理功能和系统资源区域导航
    Sqlite 管理工具 SQLiteDeveloper及破解
    android建立Menu详解
  • 原文地址:https://www.cnblogs.com/w-wanglei/p/3360447.html
Copyright © 2011-2022 走看看