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.
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.Removing IsEnabled and setting the TextBox as ReadOnly would allow you to select the text but stop user input.
The only issue with this approach is that although you won't be able to type in the TextBox it will still look 'Enabled'.
To get round that (if you want to?) you can just add a style to lighten the text and darken the background (to make it look disabled).
I've added the following example with a style that will flick the textbox between a disabled and enabled look.
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.
Slightly modified example - to match style of WinForms (not invent your own new style)
And of course your textbox must have IsReadOnly="True" attribute set.