13

I have a ListBox showing items using the following DataTemplate:

<DataTemplate x:Key="PersonTemplate" DataType="{x:Type DAL:ResultItem}" >
  <StackPanel Width="280" >
    <TextBox BorderThickness="0" IsReadOnly="True" Background="Transparent" Text="{Binding FullName1, Mode=OneWay}"/>
    ... 
  </StackPanel>
</DataTemplate>

I am using a transparent, read-only, borderless TextBox as opposed to a TextBlock because I want users to be able to select the text for copying. Should I do it differently? How can I write this so that when the user clicks on the TextBox, the ListBoxItem gets selected as well?

Thanks!

H.B.
  • 166,899
  • 29
  • 327
  • 400
Gus Cavalcanti
  • 10,527
  • 23
  • 71
  • 104

1 Answers1

22

I found that the answer is just to do this from the ListBoxItem standpoint, adding the following to its DataTemplate:

<Style.Triggers>
  <Trigger Property="IsKeyboardFocusWithin" Value="True">
    <Setter Property="IsSelected" Value="True"/>
  </Trigger>
</Style.Triggers>
Gus Cavalcanti
  • 10,527
  • 23
  • 71
  • 104
  • 1
    Just what I was looking for! Once I figured out to put that in the ItemContainerStyle of the listbox it's all good to go! – David Sep 27 '09 at 19:42
  • 3
    For full XAML, check this out http://stackoverflow.com/questions/2960098/set-listboxitem-isselected-when-child-textbox-is-focused/2962961#2962961 – iraSenthil Jun 12 '11 at 15:11