James Esh
James Esh

Reputation: 2309

Select all text in TextBox for all textboxes in app

I would like to have all the textboxes in my Windows 10 universal app to automaticly select all the text when they are focused. Much like this here (WPF). Is this possible in UWP?

Upvotes: 2

Views: 1821

Answers (2)

kirotab
kirotab

Reputation: 1306

I'd do it with attached behavior using AttachedProperty

Like this: A class to hold the property which is of type bool and on change is attaching/detaching handler for the focus event

public static class TextBoxAttached
{
    public static bool GetAutoSelectable(DependencyObject obj)
    {
        return (bool)obj.GetValue(AutoSelectableProperty);
    }

    public static void SetAutoSelectable(DependencyObject obj, bool value)
    {
        obj.SetValue(AutoSelectableProperty, value);
    }

    public static readonly DependencyProperty AutoSelectableProperty =
        DependencyProperty.RegisterAttached(
            "AutoSelectable",
            typeof(bool),
            typeof(TextBoxAttached),
            new PropertyMetadata(false, AutoFocusableChangedHandler));

    private static void AutoFocusableChangedHandler(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if(e.NewValue != e.OldValue)
        {
            if((bool)e.NewValue == true)
            {
                (d as TextBox).GotFocus += OnGotFocusHandler;
            }
            else
            {
                (d as TextBox).GotFocus -= OnGotFocusHandler;
            }
        }
    }

    private static void OnGotFocusHandler(object sender, RoutedEventArgs e)
    {
        (sender as TextBox).SelectAll();
    }
}

Usage: XAML

<TextBox Text="Test" local:TextBoxAttached.AutoSelectable="True"/>

EDIT

You could define also a default style to make all your TextBoxes be auto selectable

<Page.Resources>
    <Style TargetType="TextBox" >
        <Setter Property="local:TextBoxAttached.AutoSelectable" Value="True" />
    </Style>
</Page.Resources> 

Upvotes: 7

Igor Ralic
Igor Ralic

Reputation: 15006

There are different ways to accomplish this, but here's one: just extend the existing TextBox control and use that one everywhere.

public class SelectableTextBox : TextBox
{
    public SelectableTextBox()
    {
        this.GotFocus += SelectableTextBox_GotFocus;
    }

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

XAML:

<local:SelectableTextBox Text="This is some text."/>

Here's one other way. Write a behavior!

public class SelectAllTextBoxBehavior : DependencyObject, IBehavior
{
    private TextBox textBox;

    public DependencyObject AssociatedObject
    {
        get
        {
            return this.textBox;
        }
    }

    public void Attach(DependencyObject associatedObject)
    {
        if (associatedObject is TextBox)
        {
            this.textBox = associatedObject as TextBox;
            this.textBox.GotFocus += TextBox_GotFocus;
        }
    }

    public void Detach()
    {
        if (this.textBox != null)
        {
            this.textBox.GotFocus -= TextBox_GotFocus;
        }
    }

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

And attach it to every TextBox.

<TextBox Text="This is some text.">
    <i:Interaction.Behaviors>
        <local:SelectAllTextBoxBehavior />
    </i:Interaction.Behaviors>
</TextBox>

You will need the Behaviors SDK added to your project for the second option to work.

Upvotes: 3

Related Questions