Files
HandyControl/Net40/HandyControl_Net40/Controls/Attach/WindowAttach.cs
T
2019-01-21 11:12:38 +08:00

41 lines
1.4 KiB
C#

using System.Windows;
using System.Windows.Input;
using HandyControl.Data;
namespace HandyControl.Controls
{
public static class WindowAttach
{
public static readonly DependencyProperty IsDragElementProperty = DependencyProperty.RegisterAttached(
"IsDragElement", typeof(bool), typeof(WindowAttach), new PropertyMetadata(ValueBoxes.FalseBox, OnIsDragElementChanged));
public static void SetIsDragElement(DependencyObject element, bool value)
=> element.SetValue(IsDragElementProperty, value);
public static bool GetIsDragElement(DependencyObject element)
=> (bool) element.GetValue(IsDragElementProperty);
private static void OnIsDragElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is UIElement ctl)
{
if ((bool)e.NewValue)
{
ctl.MouseLeftButtonDown += DragElement_MouseLeftButtonDown; ;
}
else
{
ctl.MouseLeftButtonDown -= DragElement_MouseLeftButtonDown;
}
}
}
private static void DragElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (sender is DependencyObject obj && e.ButtonState == MouseButtonState.Pressed)
{
System.Windows.Window.GetWindow(obj)?.DragMove();
}
}
}
}