Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically focusing parts of a ListBoxItem on selection
    text
    copied!<p>I have a ListBox which is populated from a collection of ViewModels, which uses in place editing, which I do by having a couple of styles which I can apply to parts of the DataTemplate which make them visible/collapsed as required. These look something like:</p> <pre><code>&lt;Style x:Key="UnselectedVisibleStyle" TargetType="{x:Type FrameworkElement}"&gt; &lt;Setter Property="Visibility" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Converter={StaticResource boolToVis}, ConverterParameter=False}" /&gt; &lt;/Style&gt; &lt;Style x:Key="SelectedVisibleStyle" TargetType="{x:Type FrameworkElement}"&gt; &lt;Setter Property="Visibility" Value="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Converter={StaticResource boolToVis}, ConverterParameter=True}" /&gt; &lt;/Style&gt; </code></pre> <p>With my ListBox having it's ItemTemplate given by something like:</p> <pre><code> &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;TextBlock Text="{Binding Name}" Style="{StaticResource UnselectedVisibleStyle}" /&gt; &lt;TextBox x:Name="textBox" Text="{Binding Name}" Style="{StaticResource SelectedVisibleStyle}" /&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; </code></pre> <p>This works fine, but what I want ideally is to have the TextBox automatically selected when a user clicks the item, ideally in a nice generic way I can use throughout my project, and without too much messing around in my codebehind.</p> <p>Thanks, DM.</p>
 

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