Доработара WinUI реализация.
This commit is contained in:
168
Lattice.UI.DragDrop.WinUI/DragDropProperties.cs
Normal file
168
Lattice.UI.DragDrop.WinUI/DragDropProperties.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using Microsoft.UI.Xaml;
|
||||
|
||||
namespace Lattice.UI.DragDrop.WinUI;
|
||||
|
||||
/// <summary>
|
||||
/// Предоставляет attached properties для настройки drag-and-drop поведения элементов WinUI.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Этот класс содержит attached properties, которые позволяют включать и настраивать
|
||||
/// возможности перетаскивания и сброса для любых FrameworkElement в приложении WinUI.
|
||||
/// </remarks>
|
||||
public static class DragDropProperties
|
||||
{
|
||||
#region Drag Source Properties
|
||||
|
||||
/// <summary>
|
||||
/// Прикрепленное свойство для включения перетаскивания.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsDragSourceProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"IsDragSource",
|
||||
typeof(bool),
|
||||
typeof(DragDropProperties),
|
||||
new PropertyMetadata(false, OnIsDragSourceChanged));
|
||||
|
||||
/// <summary>
|
||||
/// Прикрепленное свойство для данных перетаскивания.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty DragDataProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"DragData",
|
||||
typeof(object),
|
||||
typeof(DragDropProperties),
|
||||
new PropertyMetadata(null));
|
||||
|
||||
/// <summary>
|
||||
/// Получает значение IsDragSource.
|
||||
/// </summary>
|
||||
public static bool GetIsDragSource(UIElement element) =>
|
||||
(bool)element.GetValue(IsDragSourceProperty);
|
||||
|
||||
/// <summary>
|
||||
/// Устанавливает значение IsDragSource.
|
||||
/// </summary>
|
||||
public static void SetIsDragSource(UIElement element, bool value) =>
|
||||
element.SetValue(IsDragSourceProperty, value);
|
||||
|
||||
/// <summary>
|
||||
/// Получает значение DragData.
|
||||
/// </summary>
|
||||
public static object? GetDragData(UIElement element) =>
|
||||
element.GetValue(DragDataProperty);
|
||||
|
||||
/// <summary>
|
||||
/// Устанавливает значение DragData.
|
||||
/// </summary>
|
||||
public static void SetDragData(UIElement element, object? value) =>
|
||||
element.SetValue(DragDataProperty, value);
|
||||
|
||||
private static void OnIsDragSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is FrameworkElement element)
|
||||
{
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
var data = GetDragData(element);
|
||||
Services.WinUIDragDropManager.Instance.MakeDragSource(element, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
Services.WinUIDragDropManager.Instance.RemoveDragSource(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Drop Target Properties
|
||||
|
||||
/// <summary>
|
||||
/// Прикрепленное свойство для включения цели сброса.
|
||||
/// </summary>
|
||||
public static readonly DependencyProperty IsDropTargetProperty =
|
||||
DependencyProperty.RegisterAttached(
|
||||
"IsDropTarget",
|
||||
typeof(bool),
|
||||
typeof(DragDropProperties),
|
||||
new PropertyMetadata(false, OnIsDropTargetChanged));
|
||||
|
||||
/// <summary>
|
||||
/// Получает значение IsDropTarget.
|
||||
/// </summary>
|
||||
public static bool GetIsDropTarget(UIElement element) =>
|
||||
(bool)element.GetValue(IsDropTargetProperty);
|
||||
|
||||
/// <summary>
|
||||
/// Устанавливает значение IsDropTarget.
|
||||
/// </summary>
|
||||
public static void SetIsDropTarget(UIElement element, bool value) =>
|
||||
element.SetValue(IsDropTargetProperty, value);
|
||||
|
||||
private static void OnIsDropTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
||||
{
|
||||
if (d is FrameworkElement element)
|
||||
{
|
||||
if ((bool)e.NewValue)
|
||||
{
|
||||
Services.WinUIDragDropManager.Instance.MakeDropTarget(element);
|
||||
}
|
||||
else
|
||||
{
|
||||
Services.WinUIDragDropManager.Instance.RemoveDropTarget(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helper Methods
|
||||
|
||||
/// <summary>
|
||||
/// Включает перетаскивание для элемента с указанными данными.
|
||||
/// </summary>
|
||||
/// <param name="element">Элемент, для которого включается перетаскивание.</param>
|
||||
/// <param name="dragData">Данные для перетаскивания. Если не указано, используются DataContext или Tag элемента.</param>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// myElement.EnableDrag(item);
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static void EnableDrag(this FrameworkElement element, object? dragData = null)
|
||||
{
|
||||
if (dragData != null)
|
||||
{
|
||||
SetDragData(element, dragData);
|
||||
}
|
||||
SetIsDragSource(element, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Отключает перетаскивание для элемента.
|
||||
/// </summary>
|
||||
/// <param name="element">Элемент, для которого отключается перетаскивание.</param>
|
||||
public static void DisableDrag(this FrameworkElement element)
|
||||
{
|
||||
SetIsDragSource(element, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Включает возможность сброса для элемента.
|
||||
/// </summary>
|
||||
/// <param name="element">Элемент, для которого включается возможность сброса.</param>
|
||||
public static void EnableDrop(this FrameworkElement element)
|
||||
{
|
||||
SetIsDropTarget(element, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Отключает возможность сброса для элемента.
|
||||
/// </summary>
|
||||
/// <param name="element">Элемент, для которого отключается возможность сброса.</param>
|
||||
public static void DisableDrop(this FrameworkElement element)
|
||||
{
|
||||
SetIsDropTarget(element, false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user