窗口初始化后聚焦文本框并选择所有文本

问题描述 投票:0回答:3

打开新窗口时,我想聚焦特定文本框并选择其中的整个文本。 我根据本教程进行了尝试:https://blogs.msdn.microsoft.com/argumentnullexceptionblogpost/2013/04/12/a-simple-selectall-behavior-for-textboxes/

为了使元素聚焦,我在网格中使用它:

<Grid d:DataContext="{StaticResource DesignTimeLayerViewModel1}" FocusManager.FocusedElement="{Binding ElementName=LayerNameInput}"> 

并尝试了交互行为:

<TextBox x:Name="LayerNameInput"
    Text="{Binding MapLayerName, UpdateSourceTrigger=PropertyChanged}"
    VerticalContentAlignment="Center"
    Width="240">
    <i:Interaction.Behaviors>
        <behaviors:SelectAllTextBoxBehavior></behaviors:SelectAllTextBoxBehavior>
    </i:Interaction.Behaviors>
</TextBox>

行为守则:

public class SelectAllTextBoxBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.GotFocus += this.OnTextBoxGotFocus;
    }

    protected override void OnDetaching()
    {
        this.AssociatedObject.GotFocus -= this.OnTextBoxGotFocus;
        base.OnDetaching();
    }

    private void OnTextBoxGotFocus(object sender, RoutedEventArgs e)
    {
        this.AssociatedObject.SelectAll();
    }
}

问题在于绑定。创建窗口时,行为会正确触发,但实际上 TextBox 中没有文本。然后,初始化 TextBox 并将文本设置为绑定变量的值,并且选择将丢失。 如果我通过多次使用 Tab 来重新聚焦文本框,它就可以正常工作。

如何在窗口创建时聚焦文本框并选择其整个文本?背后没有大量代码吗?

提前致谢!

c# wpf xaml textbox focus
3个回答
0
投票

你可以使用“window_loaded”事件来聚焦你的文本框 这是一个例子:

    private void window_Loaded(object sender, RoutedEventArgs e)
    {
        textBox.Focus();
        textBox.SelectAll();
    }

0
投票

我通过解决方法解决了这个问题。当在窗口启动期间设置 TextBox 的初始文本时,将触发 OnTextBoxTextChanged 事件。我只是抓住它,选择文本,然后取消事件的连接。

与您的答案《黑暗圣堂武士》相比,这样做的好处是,当我再次聚焦文本框时,例如使用选项卡,再次选择整个文本。

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.GotFocus += OnTextBoxGotFocus;
        AssociatedObject.TextChanged += OnTextBoxTextChanged;
    }

    protected override void OnDetaching()
    {
        AssociatedObject.GotFocus -= OnTextBoxGotFocus;
        AssociatedObject.TextChanged -= OnTextBoxTextChanged;
        base.OnDetaching();
    }

    private void OnTextBoxGotFocus(object sender, RoutedEventArgs e)
    {
        AssociatedObject.SelectAll();
    }

    private void OnTextBoxTextChanged(object sender, RoutedEventArgs e)
    {
        AssociatedObject.SelectAll();
        AssociatedObject.TextChanged -= OnTextBoxTextChanged;
    }

0
投票

我创建了一个附加属性来执行此操作。

public static class TextBoxExtensions
{
    /// <summary>
    /// An attached property that, when set to true, causes a TextBox to select
    /// all its text when it receives focus. Assumes that the TextBox's lifecycle
    /// matches its parent user control's lifecycle.
    /// </summary>
    public static readonly DependencyProperty SelectTextOnFocusProperty = DependencyProperty.RegisterAttached(
        "SelectTextOnFocus",
        typeof(bool),
        typeof(TextBoxExtensions),
        new PropertyMetadata(false, OnSelectTextOnFocusChanged));

    public static bool GetSelectTextOnFocus(TextBox textBox) => (bool)textBox.GetValue(SelectTextOnFocusProperty);

    public static void SetSelectTextOnFocus(TextBox textBox, bool value) => textBox.SetValue(SelectTextOnFocusProperty, value);

    private static void OnSelectTextOnFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is TextBox textBox && e.NewValue is bool b)
        {
            if (b) textBox.GotFocus += TextBox_GotFocus;
            else textBox.GotFocus -= TextBox_GotFocus;
        }

        void TextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            textBox.SelectAll();
        }
    }
}

您在 XAML 中声明扩展的命名空间,我使用“ext”作为我的扩展。

然后,要应用它,只需设置属性即可。

<TextBox x:Name="_quantity" ext:TextBoxExtensions.SelectTextOnFocus="True" Grid.Column="1" Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" />

如果您只是在一个地方执行此操作,那就太过分了,但是如果您有一个表单,您希望在许多文本框中执行此操作,那么使用一种样式就可以轻松实现。

© www.soinside.com 2019 - 2024. All rights reserved.