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

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

问题描述

我有一个使用以下DataTemplate显示项目的列表框:

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>

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

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