WPF单击ListBoxItem内的控件不会选择ListBoxItem

met*_*man 2 c# wpf xaml mvvm wpf-controls

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

<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>
Run Code Online (Sandbox Code Playgroud)

Nit*_*tin 5

您可以通过将以下内容ItemContainerStyle应用于ListBox使用Triggeron属性IsKeyboardFocusWithin来选择它来实现。

<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>
Run Code Online (Sandbox Code Playgroud)