Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
953 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...