文本框焦点上的 ListBoxItem 选择 [英] ListBoxItem selection on TextBox focus

查看:19
本文介绍了文本框焦点上的 ListBoxItem 选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用以下 DataTemplate 显示项目的 ListBox:

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>

我使用透明、只读、无边框的 TextBox 而不是 TextBlock,因为我希望用户能够选择要复制的文本.我应该采取不同的做法吗?我该如何编写,以便当用户单击 TextBox 时,ListBoxItem 也会被选中?

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?

谢谢!

推荐答案

我发现答案只是从ListBoxItem的角度来做,在它的DataTemplate中添加以下内容:

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>

这篇关于文本框焦点上的 ListBoxItem 选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆