翼度科技»论坛 编程开发 .net 查看内容

Avalonia 列表拖拽替换

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
实现目标,在一个ListBox中选择一个子项进行拖拽到另一个ListBox中,拖拽到某一子项区域进行替换
axaml代码
  1. 1  <ListBox
  2. 2                             Name="consumableListBox"
  3. 3                             Margin="5"
  4. 4                             ItemsSource="{Binding ConsumableList}"
  5. 5                             SelectionMode="Single">
  6. 6                             <ListBox.ItemTemplate>
  7. 7                                 <DataTemplate>
  8. 8                                     <StackPanel Margin="5,5,5,0">
  9. 9                                         <Border
  10. 10                                             Width="160"
  11. 11                                             Height="100"
  12. 12                                             Margin="0,0,0,5"
  13. 13                                             HorizontalAlignment="Center"
  14. 14                                             Background="Red"
  15. 15                                             CornerRadius="5" />
  16. 16                                         <TextBlock HorizontalAlignment="Center" Text="{Binding}" />
  17. 17                                     </StackPanel>
  18. 18                                 </DataTemplate>
  19. 19                             </ListBox.ItemTemplate>
  20. 20                         </ListBox>
复制代码
源ListBox
  1. <ListBox
  2.                 Name="platePositionListBox"
  3.                 Margin="5"
  4.                 ItemsSource="{Binding PlatePositionList}"
  5.                 SelectionMode="Single">
  6.                 <ListBox.ItemTemplate>
  7.                     <DataTemplate>
  8.                         <StackPanel Margin="5,5,5,0">
  9.                             <Border
  10.                                 Width="160"
  11.                                 Height="100"
  12.                                 Margin="0,0,0,5"
  13.                                 HorizontalAlignment="Center"
  14.                                 Background="Red"
  15.                                 CornerRadius="5" />
  16.                             <TextBlock HorizontalAlignment="Center" Text="{Binding}" />
  17.                         </StackPanel>
  18.                     </DataTemplate>
  19.                 </ListBox.ItemTemplate>
  20.             </ListBox>
复制代码
目标ListBox给源ListBox添加指针移动事件
  1. 1 private void SourceList_PointerMoved(object sender, PointerEventArgs e)
  2. 2     {
  3. 3         // 当拖拽操作开始时,在源列表中开始拖拽
  4. 4         if (e.GetCurrentPoint(consumableListBox).Properties.IsLeftButtonPressed)
  5. 5         {
  6. 6             DataObject dataObject = new DataObject();
  7. 7             dataObject.Set("dataObject", consumableListBox.SelectedItem);
  8. 8             DragDrop.DoDragDrop(e, dataObject, DragDropEffects.Move);
  9. 9         }
  10. 10     }
复制代码
源ListBox指针移动 将目标ListBox设为允许拖入
DragDrop.SetAllowDrop(platePositionListBox, true); 
由于ListBox没有DropEvent事件,需要进行添加事件
 platePositionListBox.AddHandler(DragDrop.DropEvent, PlatePositionListBox_DropEvent, RoutingStrategies.Bubble | RoutingStrategies.Direct); 
对应事件实现,其中需要获取到需要替换的项的下标,本处是通过定位进行计算
[code] 1 private void PlatePositionListBox_DropEvent(object? sender, DragEventArgs e) 2     { 3         var a = e.Data.Get("dataObject"); 4         if (e.Data.Contains("dataObject") && a != null) 5         { 6             var targetIndex = GetDropTargetIndex(platePositionListBox, e.GetPosition(platePositionListBox)); 7             if (targetIndex >= 0) 8             { 9                 if (this.DataContext is PlanViewModel viewModel)10                 {11                     viewModel.PlatePositionList.RemoveAt(targetIndex);12                     viewModel.PlatePositionList.Insert(targetIndex, a.ToString());13                 }14             }15         }16     }17 18 19 //获取目标项下标20 private int GetDropTargetIndex(ListBox targetListBox, Avalonia.Point position)21     {22         var itemsControl = targetListBox;23 24         var itemsPoint = GetAllItemDistances(targetListBox);25         var firstItemPanel = (StackPanel)Avalonia.VisualTree.VisualExtensions.FindDescendantOfType(itemsControl);26         var itemContainer = (Control)firstItemPanel;27         var firstItemBounds = itemContainer.Bounds;28         var items = itemsControl.Items;29         var y = firstItemBounds.Y;30         for (int i = 0; i < items.Count; i++)31         {32             if (itemsPoint.Index == -1)33             {34                 continue;35             }36             if (position.Y >= itemsPoint.DistanceToTop && position.Y

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具