我真的很困惑为什么这不是一个流行的问题。TextBox 的填充行为肯定是反直觉的。
查看了 Microsoft 引用资源中 TextBox 选择代码,但发现注入修复过于复杂。因此,附加属性行为似乎是最简单的方法来实现这一点。
xaml:
<TextBox Padding="24 0" local:TextBoxSelectionBehavior.IsEnabled="True"/>
c#:
public static class TextBoxSelectionBehavior
{
public const double MAX_DISTANCE = 10;
private static TextBox control;
private static Point pos;
private static int start;
private static int cur;
private static double right;
private static bool drag;
private const double MAXDRAG = 12;
public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(TextBoxSelectionBehavior), new UIPropertyMetadata(false, IsEnabledChanged));
public static bool GetIsEnabled(FrameworkElement obj) => (bool)obj.GetValue(IsEnabledProperty);
public static void SetIsEnabled(FrameworkElement obj, bool value) => obj.SetValue(IsEnabledProperty, value);
private static void IsEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var v = (TextBox)d;
if ((bool)e.NewValue)
{
v.PreviewMouseDown += V_PreviewMouseDown;
v.PreviewMouseUp += V_PreviewMouseUp;
v.PreviewMouseMove += V_PreviewMouseMove;
v.LostFocus += V_LostFocus;
v.IsKeyboardFocusedChanged += V_IsKeyboardFocusedChanged;
}
else
{
v.PreviewMouseDown -= V_PreviewMouseDown;
v.PreviewMouseUp -= V_PreviewMouseUp;
v.PreviewMouseMove -= V_PreviewMouseMove;
v.LostFocus -= V_LostFocus;
v.IsKeyboardFocusedChanged -= V_IsKeyboardFocusedChanged;
}
}
public static readonly DependencyProperty ClickSelectsProperty = DependencyProperty.RegisterAttached("ClickSelects", typeof(bool), typeof(TextBoxSelectionBehavior), new UIPropertyMetadata(true));
public static bool GetClickSelects(FrameworkElement obj) => (bool)obj.GetValue(ClickSelectsProperty);
public static void SetClickSelects(FrameworkElement obj, bool value) => obj.SetValue(ClickSelectsProperty, value);
private static void V_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (e.ChangedButton != MouseButton.Left) return;
if (control != null) { control.ReleaseMouseCapture(); control = null; }
if (e.OriginalSource is Grid g && (System.Windows.Media.VisualTreeHelper.GetParent(g) as FrameworkElement)?.Name == "PART_ContentHost")
{
control = (TextBox)sender;
pos = e.GetPosition(control);
var r = control.FindChild<ScrollContentPresenter>().Content as FrameworkElement;
right = r.TranslatePoint(new Point(r.ActualWidth, 0), control).X;
cur = start = control.GetCharacterIndexFromPoint(pos, true) + (pos.X >= right ? 1 : 0);
control.SelectionStart = start;
control.SelectionLength = 0;
control.Focus();
control.CaptureMouse();
drag = false;
e.Handled = true;
}
}
private static void V_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (control == null) return;
var p = e.GetPosition(control);
cur = control.GetCharacterIndexFromPoint(p, true);
control.SelectionStart = Math.Min(start, cur);
control.SelectionLength = Math.Abs(cur - start) + (p.X >= right ? pos.X >= right ? -1 : 1 : 0);
if (!drag) drag = Math.Max(Math.Abs(pos.X - p.X), Math.Abs(pos.Y - p.Y)) > MAXDRAG;
e.Handled = true;
}
private static void V_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (control == null) return;
control.ReleaseMouseCapture();
if (!drag && GetClickSelects(control)) control.SelectAll();
e.Handled = true;
control = null;
}
private static void V_LostFocus(object sender, RoutedEventArgs e)
{
if (control != null && control != sender) return;
if (control != null) control.ReleaseMouseCapture();
control = null;
}
private static void V_IsKeyboardFocusedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (control != null && control != sender) return;
if ((bool)e.NewValue) return;
if (control != null) control.ReleaseMouseCapture();
control = null;
}
}
而且这个附加属性可以很容易地由一个通用的样式设置器全局分配给TextBoxes。