WPF 单击 ListBoxItem 内的控件不选择 ListBoxItem [英] WPF Click on control inside ListBoxItem does not select ListBoxItem

查看:45
本文介绍了WPF 单击 ListBoxItem 内的控件不选择 ListBoxItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何类似的问题,所以我发布了新问题.在下面的代码中,我使用 ListBoxItems 创建了 ListBox 控件,其中每个控件都包含单选按钮.当我单击单选按钮时,它会选择但父 ListBoxItem 没有(ListBoxItem 未突出显示).我该如何解决这个问题?

Hi I could not find any similar problem so I posted new question. In code below I create ListBox control with ListBoxItems that each contains radio button inside. When I click on the radio button it gets selects but parent ListBoxItem does not (ListBoxItem is not highlighted). How can I solve this issue?

<ListBox Margin="0, 5, 0, 0" ItemsSource="{Binding mySource, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!-- Rabio template -->
            <RadioButton GroupName="radiosGroup"
                     Margin="10, 2, 5, 2"
                     Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.SelectedSetting}"
                     CommandParameter="{Binding SomeId, Mode=OneWay}"
                     Content="{Binding FileNameWithoutExtensions, Mode=OneWay}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

推荐答案

您可以通过将以下 ItemContainerStyle 应用到使用 Trigger<的 ListBox 来实现此目的/code> 在属性 IsKeyboardFocusWithin 上选择它.

You can achieve this by applying the following ItemContainerStyle to your ListBox which uses Trigger on property IsKeyboardFocusWithin to select it.

<ListBox>
    <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
         <Style.Triggers> 
           <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
              <Setter Property="IsSelected" Value="True"/> 
           </Trigger> 
         </Style.Triggers>
       </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

这篇关于WPF 单击 ListBoxItem 内的控件不选择 ListBoxItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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