Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to select all the text when the edit textbox in a DataGridTemplateColumn receives focus?
    text
    copied!<p>I'm trying to get a DataGridTemplateColumn to behave identically to a TextColumn </p> <ul> <li>when the cell goes into edit mode (Press F2), the user can immediately start typing in the new value</li> <li>by default, existing text content is selected - so that you can set new values easily</li> </ul> <p>Got the first one done ; however selecting all the text isn't working. As mentioned by a number of posts, tried hooking into the GotFocus event and selecting all the text in code-behind. This worked for a standalone textbox ; however for a Textbox which is the edit control for a TemplateColumn, this doesn't work.</p> <p>Any ideas? Code Sample:</p> <pre><code>&lt;Window.Resources&gt; &lt;Style x:Key="HighlightTextBoxStyle" TargetType="{x:Type TextBox}"&gt; &lt;EventSetter Event="GotFocus" Handler="SelectAllText"/&gt; &lt;EventSetter Event="GotMouseCapture" Handler="SelectAllText"/&gt; &lt;Setter Property="Background" Value="AliceBlue"/&gt; &lt;/Style&gt; &lt;DataTemplate x:Key="DefaultTitleTemplate"&gt; &lt;TextBlock Text="{Binding Title}"/&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="EditTitleTemplate"&gt; &lt;TextBox x:Name="Fox" FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" Text="{Binding Path=Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource HighlightTextBoxStyle}"&gt; &lt;/TextBox&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; &lt;DockPanel&gt; &lt;TextBox DockPanel.Dock="Top" x:Name="Test" Text="{Binding Path=(FocusManager.FocusedElement).Name, ElementName=MyWindow}" Style="{StaticResource HighlightTextBoxStyle}"/&gt; &lt;toolkit:DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False"&gt; &lt;toolkit:DataGrid.Columns&gt; &lt;toolkit:DataGridTemplateColumn Header="Templated Title" CellTemplate="{StaticResource DefaultTitleTemplate}" CellEditingTemplate="{StaticResource EditTitleTemplate}" /&gt; &lt;toolkit:DataGridTextColumn Header="Title" Binding="{Binding Path=Title}" /&gt; &lt;/toolkit:DataGrid.Columns&gt; &lt;/toolkit:DataGrid&gt; &lt;/DockPanel&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload