选项卡上的文本框SelectAll,但不是鼠标单击

本文关键字:鼠标 单击 SelectAll 文本 选项 | 更新日期: 2024-09-25 00:14:12

假设我有一个带有多个文本框的WPF表单,如果你点击文本框,它已经有了内容,我想选择该框中的所有文本,这样键入就会删除该文本。如果你用鼠标点击方框,这可能意味着你想在某个地方更改一个字母,所以在这种情况下不要全部突出显示。似乎很容易,但到目前为止,我还没有找到一个好的解决方案。以下是我迄今为止所拥有的非常接近工作的解决方案,但并不完全完美。

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <EventSetter Event="GotKeyboardFocus" Handler="EventSetter_OnHandler" />
</Style>

private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
    TextBox txt = sender as TextBox;
    if (txt != null) txt.SelectAll();
}

因此,当框获得键盘焦点时,它会选择所有文本,因此切换到文本框可以完美地选择所有文本。但是,如果鼠标单击,也会调用此方法,它也会高亮显示文本,但单击会将光标放在鼠标单击后的位置。因此,它在功能上是完美的,但当鼠标点击时,它会闪烁选择所有内容,这仍然让我感到困扰。有什么更好的方法可以做到这一点,或者在我的活动中进行某种检查,以知道我是通过鼠标点击而不是选项卡获得键盘焦点的?

选项卡上的文本框SelectAll,但不是鼠标单击

遗憾的是,到目前为止还没有看到任何干净的解决方案,您可以做的一件事就是检查鼠标状态:

var tb = (TextBox)sender;
if (Mouse.LeftButton != MouseButtonState.Pressed)
    tb.SelectAll();

但实际上有一个更好的方法,当焦点转移到按键按下时,你可以检查键盘。我建议使用GotKeyboardFocus处理程序的适当签名来获得适当的事件参数:

private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    if (e.KeyboardDevice.IsKeyDown(Key.Tab))
        ((TextBox)sender).SelectAll();
}

在这一点上,您可能仍然会看到一些选择在单击时被清除,但这只是因为前一个选择只有在未聚焦时才会被隐藏。您可以始终清除LostKeyboardFocus中的选择以防止这种情况发生(例如((TextBox)sender).Select(0, 0))。

当焦点事件发生时,您可以尝试检查鼠标是否存在于TextBox中,并检查鼠标按钮状态。这并不完美,但应该接近你想要的。

private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
    TextBox txt = sender as TextBox;
    Point position = Mouse.GetPosition(txt);
    // if Mouse position is not in the TextBox Client Rectangle
    // and Mouse Button not Pressed.
    if((!(new Rect(0,0,txt.Width,txt.Height)).Contains(position)) || ( Mouse.LeftButton != MouseButtonState.Pressed))
        if (txt != null) txt.SelectAll();
}

正如H.B.所指出的,你可以尝试使用txt。IsMouseOver属性来确定光标是否在TextBox的客户端矩形内。它看起来干净多了。

private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
    TextBox txt = sender as TextBox;
    if( !txt.IsMouseOver || Mouse.LeftButton != MouseButtonState.Pressed)
        if (txt != null) txt.SelectAll();
}

可以捕获最后按下的键,并在事件中与之进行比较

private Key lastKey;
protected override void OnKeyDown(KeyEventArgs e)
{
     lastKey = e.Key;
     base.OnKeyDown(e);
}

在您的活动中:

private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
    if(lastKey != Key.Tab)
        return;
    TextBox txt = sender as TextBox;
    if (txt != null) txt.SelectAll();
}

这并不完美,因为他们可能会点击选项卡(而不是选项卡进入您的控件),然后点击您的控件。但它在大多数情况下都会起作用。

您可以使用附加的行为模式

public class Behaviors
{
    public static readonly DependencyProperty SelectTextOnFocusProperty = DependencyProperty
        .RegisterAttached("SelectTextOnFocus", typeof(bool), typeof(Behaviors), new FrameworkPropertyMetadata(false, GotFocus));
    public static void SetSelectTextOnFocus(DependencyObject obj, bool value)
    {
        obj.SetValue(SelectTextOnFocusProperty, value);
    }
    private static void GotFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textbox = d as TextBox;
        if (null == textbox) return;
        textbox.GotKeyboardFocus += SelectTextOnFocus;
        textbox.GotMouseCapture += SelectTextOnFocus;
    }
    private static void SelectTextOnFocus(object sender, RoutedEventArgs e)
    {
        if (!(sender is TextBox)) return;
        ((TextBox)sender).SelectAll();
    }
}

在您的xaml中只需要

xmlns:my="clr-namespace:Namespace;assembly=Rkmax"

可以在类似的TextBox中使用

<TextBox my:Behaviors.SelectTextOnFocus="True" />

鼠标和键盘事件的所有这些工作

我搜索了很多解决方案,找到了几个要全选的解决方案但是,问题是,当我们在从文本框中选择部分文本后右键单击并进行剪切/复制时,即使我选择了部分文本,它也会选择所有文本。在这里解决这个问题。只需在键盘选择事件中添加以下代码。这对我有效。

    private static void SelectContentsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is TextBox)
        {
            TextBox textBox = d as TextBox;
            if ((e.NewValue as bool?).GetValueOrDefault(false))
            {
                textBox.GotKeyboardFocus += OnKeyboardFocusSelectText;                 
            }
            else
            {
                textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText;
            }
        }
    }

    private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e)
    {
        if (e.KeyboardDevice.IsKeyDown(Key.Tab))
            ((TextBox)sender).SelectAll();
    }