Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf: Selecting the Text in TextBox with IsReadOnly = true?

I have this TextBox. This TextBox is located in DataTemplate:

<DataTemplate x:Key="myTemplate">
    <TextBox  Text="{Binding Path=FullValue, Mode=TwoWay}" IsEnabled="False"  />
       ...

and I want to allow User to select the whole text inside it (optionally by clicking on the Textbox). And I don't want to use any code behind.

How to do that? Thanks in advance.

like image 480
theSpyCry Avatar asked Jul 27 '09 06:07

theSpyCry


2 Answers

Using the IsReadOnly property instead of IsEnabled allows the user to select text. Also, if it shouldn't be edited, a OneWay binding should be enough.

The idea of XAML isn't to completely replace code-behind. Most important is that you try to have only UI-specific code in the code-behind, not business logic. That being said, selecting all text is UI-specific and doesn't hurt in the code-behind. Use myTextBox.SelectAll() for that.

like image 90
Botz3000 Avatar answered Nov 18 '22 19:11

Botz3000


One note I just discovered (obviously this is an old question but this might help someone):

If IsHitTestVisible=False then selecting (and therefore Copy) is also disabled.

like image 7
Dave Lowther Avatar answered Nov 18 '22 21:11

Dave Lowther