qamp/Qrakhen.TilingFrames/Controls/DragAndDrop/DragAndDropControl.cs

99 lines
3.2 KiB
C#

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Qrakhen.TilingFrames.Controls.DragAndDrop;
public abstract partial class DragAndDropControl : Control
{
public virtual void OnDragStart(object model, UIElement draggedElement) { }
public virtual void OnDragEnd(object model, UIElement targetElement) { }
public virtual void OnDrag(object sender, MouseEventArgs args) { }
#region Dependency Properties
public bool AllowDrag {
get => (bool)GetValue(AllowDragProperty);
set => SetValue(AllowDragProperty, value);
}
public static DependencyProperty AllowDragProperty = DependencyProperty
.Register(
nameof(AllowDrag),
typeof(bool),
typeof(DragAndDropControl),
new PropertyMetadata(true));
#endregion
#region Attached Properties
public static readonly DependencyProperty IsDraggableProperty = DependencyProperty
.RegisterAttached(
"IsDraggable",
typeof(bool),
typeof(DragAndDropControl),
new PropertyMetadata(false, OnIsDraggableChanged));
public static bool GetIsDraggable(DependencyObject obj) => (bool)obj.GetValue(IsDraggableProperty);
public static void SetIsDraggable(DependencyObject obj, bool value) => obj.SetValue(IsDraggableProperty, value);
private static void OnIsDraggableChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender is UIElement element && (bool)e.NewValue) {
}
}
private static void HandleDragStart(object sender, MouseButtonEventArgs args, DragAndDropControl dndControl)
{
if (sender is UIElement element) {
}
}
private static void HandleDrop(object sender, MouseButtonEventArgs args, DragAndDropControl dndControl)
{
// moi schaun
}
private static T? FindVisualParent<T>(DependencyObject child) where T : DependencyObject
{
// Todo: find a better solution to this, if there even is one.
// There are, in fact, many controls even in the WPF standard that do similar lookups,
// so I assume it can't be _that_ bad, but every clean solution kind of seems to need some ugly spot in it. :(
DependencyObject parent = VisualTreeHelper.GetParent(child);
while (parent != null) {
if (parent is T expected)
return expected;
parent = VisualTreeHelper.GetParent(parent);
}
return null;
}
public static readonly DependencyProperty HandlerProperty = DependencyProperty
.RegisterAttached(
"Handler",
typeof(DependencyObject),
typeof(DragAndDropControl),
new PropertyMetadata(null, OnHandlerChanged));
public static DependencyObject GetHandler(DependencyObject obj) => (DependencyObject)obj.GetValue(HandlerProperty);
public static void SetHandler(DependencyObject obj, bool value) => obj.SetValue(HandlerProperty, value);
private static void OnHandlerChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender is UIElement element && e.NewValue is DragAndDropControl dndControl) {
}
throw new InvalidOperationException($"Can not set {e.NewValue} as handler. Handler needs to be of type DragAndDropControl.");
}
#endregion
}