using Microsoft.UI.Xaml;
namespace Lattice.UI.DragDrop.WinUI;
///
/// Предоставляет attached properties для настройки drag-and-drop поведения элементов WinUI.
///
///
/// Этот класс содержит attached properties, которые позволяют включать и настраивать
/// возможности перетаскивания и сброса для любых FrameworkElement в приложении WinUI.
///
public static class DragDropProperties
{
#region Drag Source Properties
///
/// Прикрепленное свойство для включения перетаскивания.
///
public static readonly DependencyProperty IsDragSourceProperty =
DependencyProperty.RegisterAttached(
"IsDragSource",
typeof(bool),
typeof(DragDropProperties),
new PropertyMetadata(false, OnIsDragSourceChanged));
///
/// Прикрепленное свойство для данных перетаскивания.
///
public static readonly DependencyProperty DragDataProperty =
DependencyProperty.RegisterAttached(
"DragData",
typeof(object),
typeof(DragDropProperties),
new PropertyMetadata(null));
///
/// Получает значение IsDragSource.
///
public static bool GetIsDragSource(UIElement element) =>
(bool)element.GetValue(IsDragSourceProperty);
///
/// Устанавливает значение IsDragSource.
///
public static void SetIsDragSource(UIElement element, bool value) =>
element.SetValue(IsDragSourceProperty, value);
///
/// Получает значение DragData.
///
public static object? GetDragData(UIElement element) =>
element.GetValue(DragDataProperty);
///
/// Устанавливает значение DragData.
///
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
///
/// Прикрепленное свойство для включения цели сброса.
///
public static readonly DependencyProperty IsDropTargetProperty =
DependencyProperty.RegisterAttached(
"IsDropTarget",
typeof(bool),
typeof(DragDropProperties),
new PropertyMetadata(false, OnIsDropTargetChanged));
///
/// Получает значение IsDropTarget.
///
public static bool GetIsDropTarget(UIElement element) =>
(bool)element.GetValue(IsDropTargetProperty);
///
/// Устанавливает значение IsDropTarget.
///
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
///
/// Включает перетаскивание для элемента с указанными данными.
///
/// Элемент, для которого включается перетаскивание.
/// Данные для перетаскивания. Если не указано, используются DataContext или Tag элемента.
///
///
/// myElement.EnableDrag(item);
///
///
public static void EnableDrag(this FrameworkElement element, object? dragData = null)
{
if (dragData != null)
{
SetDragData(element, dragData);
}
SetIsDragSource(element, true);
}
///
/// Отключает перетаскивание для элемента.
///
/// Элемент, для которого отключается перетаскивание.
public static void DisableDrag(this FrameworkElement element)
{
SetIsDragSource(element, false);
}
///
/// Включает возможность сброса для элемента.
///
/// Элемент, для которого включается возможность сброса.
public static void EnableDrop(this FrameworkElement element)
{
SetIsDropTarget(element, true);
}
///
/// Отключает возможность сброса для элемента.
///
/// Элемент, для которого отключается возможность сброса.
public static void DisableDrop(this FrameworkElement element)
{
SetIsDropTarget(element, false);
}
#endregion
}