TEXTBOX READONLY “ON/OFF” BETWEEN “DOUBLE CLICK AND LOST FOCUS EVENTS” IN WPF

Jayden Bell

Total Post:110

Points:774
Posted by  Jayden Bell
WPF 
WPF
 2786  View(s)
Ratings:
Rate this:

I have a control like below xaml with Read only enabled.

          <TextBox  Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Background="Transparent" IsReadOnly="True" BorderThickness="0" TextWrapping="Wrap" >  

Now when i double click this text box , i should be able to enter texts. that is my readonly should become false

If i move to another item in the window other than this text box , then i my text box should become readonly again.

I am trying in WPF Trigget. but not getting the right hint . Can anyone help me here ?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: TextBox readonly “on/off” between “double click and lost focus events” in wpf

    Hi Jayden,

    You can make this with 2 events, MouseDoubleClick and LostFocus

    <Grid>

        <TextBox IsReadOnly="True"

                 MouseDoubleClick="TextBox_MouseDoubleClick"

                 LostFocus="TextBox_LostFocus"/>

    </Grid>

    In you procedural code:

    private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)

    {

        TextBox textBox = sender as TextBox;

        textBox.IsReadOnly = false;

        //textBox.CaretIndex = textBox.Text.Count();

        textBox.SelectAll();

    }

    private void TextBox_LostFocus(object sender, RoutedEventArgs e)

    {

        TextBox textBox = sender as TextBox;

        textBox.IsReadOnly = true;

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!