如何在输入单元格时选择文本

发布于 2024-12-09 10:01:43 字数 102 浏览 1 评论 0原文

我有一个网格,当我走过其中的单元格时,我进入单元格,但不选择文本。这很烦人,因为当我需要更改值时,我需要首先使用退格键。

进入单元格后,我可以选择该单元格的内容,我该怎么办?

I have a Grid, and when I'm walking by cells of it, I enter to cell, but whithout selecting text. It's annoying because, when I need to change a value, I need firstly use Backspace key.

What can I do to after entering to cell have selected content of that cell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2024-12-16 10:01:43

我建议为此目的使用一种行为。下面我提供了一个最小的实现,它提供了我认为您正在寻找的功能:

/// <summary>
/// <see cref="Behavior{T}"/> of <see cref="TextBox"/> for selecting all text when the text box is focused
/// </summary>
public class TextBoxSelectOnFocusBehavior : Behavior<TextBox>
{
    private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBox = (sender as TextBox);

        if (textBox != null)
            textBox.SelectAll();
    }

    /// <summary>
    /// React to the behavior being attached to an object
    /// </summary>
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.GotFocus += AssociatedObject_GotFocus;
    }

    /// <summary>
    /// React to the behavior being detached from an object
    /// </summary>
    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
    }
}

希望这会有所帮助。

I would recommend the use of a behavior for this purpose. Below I have included a minimal implementation that provides the functionality I think you're looking for:

/// <summary>
/// <see cref="Behavior{T}"/> of <see cref="TextBox"/> for selecting all text when the text box is focused
/// </summary>
public class TextBoxSelectOnFocusBehavior : Behavior<TextBox>
{
    private void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox textBox = (sender as TextBox);

        if (textBox != null)
            textBox.SelectAll();
    }

    /// <summary>
    /// React to the behavior being attached to an object
    /// </summary>
    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.GotFocus += AssociatedObject_GotFocus;
    }

    /// <summary>
    /// React to the behavior being detached from an object
    /// </summary>
    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
    }
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文