使WPF TextBox边距可点击/可选

时间:2017-05-28 20:12:26

标签: c# wpf textbox

我有一个白色背景的WPF TextBox。我已经给它一些Padding,以便文本周围有一个“边距”(类似于MS Word中的边距)。

<TextBox x:Name="MyTextBox" Padding="25" />

与Word不同,文本周围的空白区域不是“活动”(请参阅​​下面的彩色编码插图)。单击其中(红色部分)不会移动文本框的插入符号,也不会开始选择。但请注意,整个绿色区域 处于活动状态 - 即使文本下方的空白区域(意味着您可以在其中单击以移动插入符号或开始选择)。 ..

textbox with colored alive/dead zones

所以,我的问题是:是否有任何方法可以在TextBox周围添加空白但空白但也是可编辑区域的一部分(这样它会对点击/选择做出反应,比如空白区域绿色部分吗?)

谢谢!

P.S。我在搜索答案时最接近的是:How to set the margin on a internal TextBoxView in wpf ...但是,增加内部Margin的{​​{1}}会增加文本周围的空间,空间似乎仍然没有任何“活跃”......似乎TextBoxView没有TextBoxView属性,所以我不能尝试(尽管我想象) PaddingPadding更有可能成为解决方案......

1 个答案:

答案 0 :(得分:0)

我真的很困惑为什么它不是一个受欢迎的问题。当然,TextBox具有违反直觉的填充行为。

查看了Microsoft Referenceourece的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;
    }
}

此附加属性可以由文本框的常规样式设置器轻松地全局分配。