Search code examples
c#wpftextboxfocusselection

WPF TextBox select content on mouse click


For WPF TextBox I need to select all content of it in case of mouse click on it (in "real life" it will be touch on touchpad monitor). For test I created simple project and check the solutions on Stackoverflow. I found several cases but they doesn't work. Please help me? My project on Github: https://github.com/cheffcook/CustomTextBox2

P.S. About OnKeyDown - I just test another functionality

ExtendedTextBox.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CustomTextBox
{
    public class ExtendedTextBox : TextBox
    {
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (TextProperty != null && e.Key == Key.Enter) 
            {
                GetBindingExpression(TextProperty)?.UpdateSource();
            }
        }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            SelectAll();
        }

        /*protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            base.OnGotKeyboardFocus(e);
            SelectAll();
        }*/

        protected override void OnMouseUp(MouseButtonEventArgs e)
        { 
            base.OnMouseUp(e);
            Focus();
        }
    }
}

MainWindow.xaml

<Window x:Class="CustomTextBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:localVM="clr-namespace:CustomTextBox"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <localVM:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        
        <Button Grid.Row="0" Grid.Column="0" Height="40" Width="80" Content="Nothing" HorizontalAlignment="Left" />
        <Label Grid.Row="1" Grid.Column="1" FontSize="20" Content="{Binding CustomValue1}" />
        <Label Grid.Row="2" Grid.Column="1" FontSize="20" Content="{Binding CustomValue2}" />
        <Label Grid.Row="3" Grid.Column="1" FontSize="20" Content="{Binding CustomValue3}" />
        <localVM:ExtendedTextBox Grid.Row="1" Grid.Column="0" FontSize="20" Text="{Binding CustomValue1}" />
        <localVM:ExtendedTextBox Grid.Row="2" Grid.Column="0" FontSize="20" Text="{Binding CustomValue2}" />
        <localVM:ExtendedTextBox Grid.Row="3" Grid.Column="0" FontSize="20" Text="{Binding CustomValue3}" />
    </Grid>
</Window>

MainViewModel.cs

using Prism.Mvvm;

namespace CustomTextBox
{
    internal class MainViewModel : BindableBase
    {
        private float _customValue1;
        public float CustomValue1
        {
            get => _customValue1;
            set => SetProperty(ref _customValue1, value, nameof(CustomValue1));
        }

        private float _customValue2;
        public float CustomValue2
        {
            get => _customValue2;
            set => SetProperty(ref _customValue2, value, nameof(CustomValue2));
        }

        private float _customValue3;
        public float CustomValue3
        {
            get => _customValue3;
            set => SetProperty(ref _customValue3, value, nameof(CustomValue3));
        }

        public MainViewModel()
        {
            CustomValue1 = 12.3f;
            CustomValue2 = 45.6f;
            CustomValue3 = 78.9f;
        }
    }
}

Solution

  • By Adding SelectAll() to OnMouseUp Event it works

    protected override void OnMouseUp(MouseButtonEventArgs e)
    { 
        base.OnMouseUp(e);
        Focus();
        SelectAll();
    }