• WPF ListView 简单的拖拽实现(转)


    首先设置ListView的AllowDrop=True;SelectionMode=Extended;并且ListView视图为GridVIew。

     1 private void listView1_MouseMove(object sender, MouseEventArgs e)  
     2       {  
     3           ListView listview = sender as ListView;  
     4           if (e.LeftButton == MouseButtonState.Pressed)  
     5           {  
     6               System.Collections.IList list = listview.SelectedItems as System.Collections.IList;  
     7               DataObject data = new DataObject(typeof(System.Collections.IList), list);  
     8               if (list.Count > 0)  
     9               {  
    10                   DragDrop.DoDragDrop(listview, data, DragDropEffects.Move);  
    11               }  
    12           }  
    13       }  
    14   
    15       private void listView1_Drop(object sender, DragEventArgs e)  
    16       {  
    17           if (e.Data.GetDataPresent(typeof(System.Collections.IList)))  
    18           {  
    19               System.Collections.IList peopleList = e.Data.GetData(typeof(System.Collections.IList)) as System.Collections.IList;  
    20               //index为放置时鼠标下元素项的索引  
    21               int index = GetCurrentIndex(new GetPositionDelegate(e.GetPosition));  
    22               if (index > -1)  
    23               {  
    24                   LogMessage Logmess = peopleList[0] as LogMessage;  
    25                   //拖动元素集合的第一个元素索引  
    26                   int OldFirstIndex = StreamHelper.List.IndexOf(Logmess);  
    27                   //下边那个循环要求数据源必须为ObservableCollection<T>类型,T为对象  
    28                   for (int i = 0; i < peopleList.Count; i++)  
    29                   {  
    30                       StreamHelper.List.Move(OldFirstIndex, index);  
    31                   }  
    32                   listView1.SelectedItems.Clear();  
    33               }  
    34           }  
    35       }  
    36   
    37       private int GetCurrentIndex(GetPositionDelegate getPosition)  
    38       {  
    39           int index = -1;  
    40           for (int i = 0; i < listView1.Items.Count; ++i)  
    41           {  
    42               ListViewItem item = GetListViewItem(i);  
    43               if (item != null && this.IsMouseOverTarget(item, getPosition))  
    44               {  
    45                   index = i;  
    46                   break;  
    47               }  
    48           }  
    49           return index;  
    50       }  
    51   
    52       private bool IsMouseOverTarget(Visual target, GetPositionDelegate getPosition)  
    53       {  
    54           Rect bounds = VisualTreeHelper.GetDescendantBounds(target);  
    55           Point mousePos = getPosition((IInputElement)target);  
    56           return bounds.Contains(mousePos);  
    57       }  
    58   
    59       delegate Point GetPositionDelegate(IInputElement element);  
    60   
    61       ListViewItem GetListViewItem(int index)  
    62       {  
    63           if (listView1.ItemContainerGenerator.Status != GeneratorStatus.ContainersGenerated)  
    64               return null;  
    65           return listView1.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;  
    66       }  

    其中StreamHelper.List是StreamHelper类里边有一个静态的ObservableCollection<LogMessage>集合名字为List;

    LogMessage就是集合里边的T对象了;

  • 相关阅读:
    训练一个图像分类器
    边界值分析
    系统测试
    单元测试
    集成测试
    为什么要调整挖矿难度
    并在session里执行graph
    pytorch处理数据
    判定表测试
    qemusystemriscv64
  • 原文地址:https://www.cnblogs.com/lijianda/p/6603654.html
Copyright © 2020-2023  润新知