Avalonia 列表拖拽替換

来源:https://www.cnblogs.com/fengxinyuan/archive/2023/08/03/17604442.html
-Advertisement-
Play Games

實現目標,在一個ListBox中選擇一個子項進行拖拽到另一個ListBox中,拖拽到某一子項區域進行替換 axaml代碼 1 <ListBox 2 Name="consumableListBox" 3 Margin="5" 4 ItemsSource="{Binding ConsumableList ...


實現目標,在一個ListBox中選擇一個子項進行拖拽到另一個ListBox中,拖拽到某一子項區域進行替換

axaml代碼

 1  <ListBox
 2                             Name="consumableListBox"
 3                             Margin="5"
 4                             ItemsSource="{Binding ConsumableList}"
 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
                Name="platePositionListBox"
                Margin="5"
                ItemsSource="{Binding PlatePositionList}"
                SelectionMode="Single">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Margin="5,5,5,0">
                            <Border
                                Width="160"
                                Height="100"
                                Margin="0,0,0,5"
                                HorizontalAlignment="Center"
                                Background="Red"
                                CornerRadius="5" />
                            <TextBlock HorizontalAlignment="Center" Text="{Binding}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
目標ListBox

給源ListBox添加指針移動事件

 1 private void SourceList_PointerMoved(object sender, PointerEventArgs e)
 2     {
 3         // 當拖拽操作開始時,在源列表中開始拖拽
 4         if (e.GetCurrentPoint(consumableListBox).Properties.IsLeftButtonPressed)
 5         {
 6             DataObject dataObject = new DataObject();
 7             dataObject.Set("dataObject", consumableListBox.SelectedItem);
 8             DragDrop.DoDragDrop(e, dataObject, DragDropEffects.Move);
 9         }
10     }
源ListBox指針移動

 將目標ListBox設為允許拖入

DragDrop.SetAllowDrop(platePositionListBox, true); 

由於ListBox沒有DropEvent事件,需要進行添加事件

 platePositionListBox.AddHandler(DragDrop.DropEvent, PlatePositionListBox_DropEvent, RoutingStrategies.Bubble | RoutingStrategies.Direct); 

對應事件實現,其中需要獲取到需要替換的項的下標,本處是通過定位進行計算

 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<StackPanel>(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[i].Index == -1)
33             {
34                 continue;
35             }
36             if (position.Y >= itemsPoint[i].DistanceToTop && position.Y <= itemsPoint[i].DistanceToTop + firstItemBounds.Height)
37             {
38                 return i;
39             }
40         }
41         return items.Count;
42     }
43 
44 //獲取目標ListBox的項距離頂部邊框的距離,如果未呈現到畫面上,下標設為-1
45 private List<ItemCoordinates> GetAllItemDistances(ListBox listBox)
46     {
47         var itemCoordinatesList = new List<ItemCoordinates>();
48         var items = listBox.Items;
49         var scrollViewer = listBox.FindDescendantOfType<ScrollViewer>();
50         var topOffset = scrollViewer.Offset.Y;
51 
52         for (int i = 0; i < items.Count; i++)
53         {
54             var itemContainer = listBox.ItemContainerGenerator.ContainerFromIndex(i) as Control;
55             if (itemContainer != null)
56             {
57                 var itemBounds = itemContainer.Bounds;
58                 var distanceToTop = itemBounds.Top - topOffset;
59                 itemCoordinatesList.Add(new ItemCoordinates(i, distanceToTop));
60             }
61             else
62             {
63                 itemCoordinatesList.Add(new ItemCoordinates(-1, -999));
64             }
65         }
66         return itemCoordinatesList;
67     }
具體實現

最終效果:

初始畫面

拖拽後:

 

本文來自博客園,作者:阿金啊,轉載請註明原文鏈接:https://www.cnblogs.com/fengxinyuan/p/17604442.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ## jsp ​ servlet 是無法將後端獲取的數據傳遞給html 頁面的,無法再servlet 中通過轉發或者是重定向的方式,給html 頁面傳遞響應的後端數據,servlet 中由於拼接過於繁瑣,是不適合寫html 的因此引入了 jsp ,既可以編寫 html標簽,也可以寫 Java 代碼, ...
  • # Go 語言入門指南: 環境搭建、基礎語法和常用特性解析 | 青訓營 ## 從零開始 ### Go 語言簡介 ![img](https://img2023.cnblogs.com/blog/2724888/202308/2724888-20230803143447307-285055892.png ...
  • 本文主要從源碼的角度解析了 ThreadLocal,並分析了發生記憶體泄漏的原因及正確用法,最後對它的應用場景進行了簡單介紹。 ...
  • 拆分列是`pandas`中常用的一種數據操作,它可以將一個包含多個值的列按照指定的規則拆分成多個新列,方便進行後續的分析和處理。拆分列的使用場景比較廣泛,以下是一些常見的應用場景: 1. 處理日期數據:在日期數據中,經常會將年、月、日等信息合併成一列,通過拆分列可以將其拆分成多個新列,方便進行時間序 ...
  • ## 教程簡介 JUnit是一個Java語言的單元測試框架。它由Kent Beck和Erich Gamma建立,逐漸成為源於Kent Beck的sUnit的xUnit家族中最為成功的一個。 JUnit有它自己的JUnit擴展生態圈。多數Java的開發環境都已經集成了JUnit作為單元測試的工具。JU ...
  • > 服務發現與負載均衡。 # 一、背景 在微服務架構中,這裡以開發環境「Dev」為基礎來描述,在K8S集群中通常會開放:路由網關、註冊中心、配置中心等相關服務,可以被集群外部訪問; ![](https://img2023.cnblogs.com/blog/1691717/202308/1691717 ...
  • ### 歡迎訪問我的GitHub > 這裡分類和彙總了欣宸的全部原創(含配套源碼):[https://github.com/zq2599/blog_demos](https://github.com/zq2599/blog_demos) ### 本篇概覽 - 本文是《quarkus依賴註入》系列的第 ...
  • # Unity Shader編輯器工具類ShaderUtil 常用函數和用法 ![Unity Shader](https://cdn.pixabay.com/photo/2017/08/30/01/05/milky-way-2695569_960_720.jpg) Unity的Shader編輯器工具 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...