Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
943 views
in Technique[技术] by (71.8m points)

wpf - ListBoxItem selection on TextBox focus

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

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...