Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select text of a texbox in wpf MVVM

Tags:

c#

mvvm

wpf

xaml

I have some control in a stackpanel.
When I right click on the panel it gives me a context menu and I can edit the text of these control.
Here I used TextBlock to display the data and TextBox to edit the data (When TextBox is visible TextBlock become collapsed and vice versa)
I want to select all the text of the TextBox and focus it when the TextBox is visible.

I tried using Interaction. But didn't work out :(
Is there any way to do this?
For example : When the TextBox is visible I can fire some command in my viewmodel and select all the text from my viewmodel.

<TextBlock Text="{Binding MachineResponseText}" Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}, ConverterParameter=true}"/>
<TextBox x:Name="MachineResponseTextBox" Text="{Binding MachineResponseText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}}">
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="IsVisible" Value="True">
                    <!--Is there any way to select all the text when this textbox is visible?-->
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
like image 812
Ali Akber Avatar asked Jan 31 '26 00:01

Ali Akber


1 Answers

If you want to use the MVVM design pattern, I would suggest do it this way.

  1. First you have to create a Extension method as below.

    public static class IsSelectTextExtension
    {
    public static readonly DependencyProperty IsSelectTextProperty = DependencyProperty.RegisterAttached("IsSelectText", typeof(bool), typeof(IsSelectTextExtension), new UIPropertyMetadata(false, OnIsSelectTextPropertyChanged));
    
    public static bool GetIsSelectText(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsSelectTextProperty);
    }
    
    public static void SetIsSelectText(DependencyObject obj, bool value)
    {
        obj.SetValue(IsSelectTextProperty, value);
    }
    
    private static void OnIsSelectTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (TextBox)d;
        if ((bool)e.NewValue)
        {
            uie.SelectAll();
        }
    }
    }
    
    1. Include the namespace to the view and in your view (.xaml file) bind bool property to the extension that we created above.

<Window x:Class="POS.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:local="clr-namespace:WpfExample"
        xmlns:extension="clr-namespace:WpfExample.Extension"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox extension:IsSelectTextExtension.IsSelectText="{Binding IsSelectText}"/>
    </Grid>
</Window>
  1. In the view model class, define IsSelectText property and change the value according to your requirement.

    public class MainWindowViewModel: INotifyPropertyChanged
    {
    private bool _isSelectText;
    public bool IsSelectText
    {
        get
        {
            return this._isSelectText;
        }
        set
        {
            _isSelectText = value;
            OnPropertyChanged(nameof(IsSelectText));
        }
    }
    
    public NewSaleViewModel()
    {
    }
    
    #region INotifyPropertyChanged Implimentations
    
    public event PropertyChangedEventHandler PropertyChanged;
    
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    
    #endregion
    }
    
like image 187
Prabodha Avatar answered Feb 01 '26 13:02

Prabodha