Files
Lattice/Lattice.UI.Docking.WinUI/Services/DragDropService.cs
2026-02-01 09:26:13 +03:00

53 lines
1.8 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Microsoft.UI.Xaml;
using System;
namespace Lattice.UI.Docking.WinUI.Services;
/// <summary>
/// Сервис для управления операциями Drag & Drop в WinUI.
/// </summary>
public static class DragDropService
{
/// <summary>
/// Настраивает элемент для поддержки перетаскивания.
/// </summary>
public static void SetupDragElement(UIElement element, Func<object?> getDataCallback)
{
element.CanDrag = true;
element.DragStarting += (sender, args) =>
{
var data = getDataCallback();
if (data != null)
{
args.Data.Properties.Add("LatticeDockElement", data);
args.Data.SetData("LatticeDockElement", data);
args.AllowedOperations = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
}
};
}
/// <summary>
/// Настраивает элемент для приема сброса.
/// </summary>
public static void SetupDropElement(UIElement element, Func<object, bool> dropCallback)
{
element.AllowDrop = true;
element.Drop += (sender, args) =>
{
if (args.DataView.Properties.TryGetValue("LatticeDockElement", out var data))
{
if (dropCallback(data))
{
args.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
}
}
};
element.DragOver += (sender, args) =>
{
args.AcceptedOperation = Windows.ApplicationModel.DataTransfer.DataPackageOperation.Move;
args.DragUIOverride.IsGlyphVisible = true;
args.DragUIOverride.Caption = "Переместить";
};
}
}