Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/AttachedProperties/SelectionProps.cs

71 lines
2.4 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner
2021-07-23 09:42:22 +08:00
{
public static class SelectionProps
{
#region EnabledForSelection
public static readonly DependencyProperty EnabledForSelectionProperty =
DependencyProperty.RegisterAttached("EnabledForSelection", typeof(bool), typeof(SelectionProps),
new FrameworkPropertyMetadata((bool)false,
new PropertyChangedCallback(OnEnabledForSelectionChanged)));
public static bool GetEnabledForSelection(DependencyObject d)
{
return (bool)d.GetValue(EnabledForSelectionProperty);
}
public static void SetEnabledForSelection(DependencyObject d, bool value)
{
d.SetValue(EnabledForSelectionProperty, value);
}
private static void OnEnabledForSelectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FrameworkElement fe = (FrameworkElement)d;
if ((bool)e.NewValue)
{
fe.PreviewMouseDown += Fe_PreviewMouseDown;
}
else
{
fe.PreviewMouseDown -= Fe_PreviewMouseDown;
}
}
#endregion
static void Fe_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
2023-01-22 21:46:59 +08:00
if (((FrameworkElement)sender).DataContext is ISelectable selectable)
2021-07-23 09:42:22 +08:00
{
2023-01-22 21:46:59 +08:00
if (selectable.IsHitTestVisible)
2021-07-23 09:42:22 +08:00
{
2023-01-22 21:46:59 +08:00
if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
2021-07-23 09:42:22 +08:00
{
2023-01-22 21:46:59 +08:00
if ((Keyboard.Modifiers & (ModifierKeys.Shift)) != ModifierKeys.None)
{
selectable.IsSelected = !selectable.IsSelected;
}
2021-07-23 09:42:22 +08:00
2023-01-22 21:46:59 +08:00
if ((Keyboard.Modifiers & (ModifierKeys.Control)) != ModifierKeys.None)
{
selectable.IsSelected = !selectable.IsSelected;
}
}
else if (!selectable.IsSelected)
2021-07-23 09:42:22 +08:00
{
2023-01-22 21:46:59 +08:00
selectable.AddToSelection(true);
2021-07-23 09:42:22 +08:00
}
}
2023-01-22 21:46:59 +08:00
}
2021-07-23 09:42:22 +08:00
}
}
}