using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml; using RustTools.muqing; using Windows.ApplicationModel.DataTransfer; using Microsoft.UI.Xaml.Input; using Windows.Storage; using RustTools.Helpers; using Microsoft.UI.Xaml.Media; using Windows.Foundation; namespace RustTools.Editor; public class EditorTreeView: TreeView { public EditorTreeView() { //CanDragItems = false; //CanReorderItems =false; DragEnter += EditorTreeView_DragEnter; DragItemsStarting += TreeView_DragItemsStarting; DragItemsCompleted += EditorTreeView_DragItemsCompleted; DragOver += TreeView_DragOver; Drop += TreeView_Drop; // 在构造函数中注册事件处理程序 } private void EditorTreeView_DragEnter(object sender, DragEventArgs e){ gj.sc("EditorTreeView_DragEnter "+e); } private void TreeView_DragItemsStarting(TreeView sender, TreeViewDragItemsStartingEventArgs e) { //gj.sc("TreeView_DragItemsStarting"+e); var items = e.Items.Cast(); var firstItem = items.FirstOrDefault(); gj.sc(firstItem.Dir); e.Data.SetData("path", firstItem.Dir); e.Data.RequestedOperation = DataPackageOperation.Move; // Use Copy to avoid moving items } private void EditorTreeView_DragItemsCompleted(TreeView sender, TreeViewDragItemsCompletedEventArgs e) { var items = e.Items.Cast(); var firstItem = items.FirstOrDefault(); gj.sc("放下了 " + firstItem.Dir); } /// /// 如果是shift则开启多选状态 /// /// /// private void treeView_KeyDown(object sender, KeyRoutedEventArgs e) { // 检查是否按下了Shift键 if (e.Key == Windows.System.VirtualKey.Shift) { gj.sc(e.Key); // 启用多选模式 SelectionMode = TreeViewSelectionMode.Multiple; } } private void treeView_KeyUp(object sender, KeyRoutedEventArgs e) { // 检查是否松开了Shift键 if (e.Key == Windows.System.VirtualKey.Shift) { // 清除当前多选模式下选中的项目 var selectedItems = SelectedItems.ToList(); foreach (var item in selectedItems) { SelectedItems.Remove(item); } gj.sc("取消"); // 取消多选模式 SelectionMode = TreeViewSelectionMode.Single; //treeView.SelectedItems.Clear(); } } /// /// 其他窗口的文件夹拖过来的例子 /// /// /// private void TreeView_DragOver(object sender, DragEventArgs e) { gj.sc(e); // Allow dragging but prevent reordering if (e.DataView.Contains(StandardDataFormats.StorageItems)) { e.AcceptedOperation = DataPackageOperation.Move; // Use Copy to avoid reordering e.Handled = true; } } /// /// 其他文件夹拖过来的放下事件 /// /// /// private async void TreeView_Drop(object sender, DragEventArgs e) { gj.sc("TreeView_Drop: " + e); // Handle drop event if needed but prevent reordering if (e.DataView.Contains(StandardDataFormats.StorageItems)) { var storageItems = await e.DataView.GetStorageItemsAsync(); // 获取目标文件夹,即拖放的位置 // 获取目标位置 var treeView = sender as TreeView; var data = await e.DataView.GetDataAsync("path"); gj.sc(data); foreach (var item in storageItems) { if (item is StorageFolder folder) { // 移动文件夹 gj.sc(folder.Path+"-->"); //var newFolder = await folder.MoveAsync(targetFolder, folder.Name, NameCollisionOption.ReplaceExisting); } else if (item is StorageFile file) { // 移动文件 //var newFile = await file.MoveAsync(targetFolder, file.Name, NameCollisionOption.ReplaceExisting); } } e.Handled = true; } } }