Wpf 列表框不';无法检测所选项目

Wpf 列表框不';无法检测所选项目,wpf,Wpf,我有以下列表框: <ListBox ItemsSource="{Binding AvailableTemplates}" Style="{DynamicResource SearchListBoxStyle}" SelectedItem="{Binding SelectedTemplate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <ListBox.ItemTemplate> <DataTe

我有以下列表框:

<ListBox ItemsSource="{Binding AvailableTemplates}" Style="{DynamicResource SearchListBoxStyle}" SelectedItem="{Binding SelectedTemplate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <RadioButton Content="{Binding}" GroupName="group" />
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>


如果我在单选按钮上选择,则不会检测到所选项目已更改。它仅检测我是否在列表框行的单选按钮下单击。单击单选按钮时如何修改以检测所选项目是否已更改?

您不会出错:listBox.SelectedItem和radioButton.IsChecked

它们是完全不同的东西, SelectedItem称为ListBoxItem,您的单选按钮位于ListBoxItem中


您必须为属性IsChecked(RadioButton)进行绑定。

您不能弄错:listBox.SelectedItem和RadioButton.IsChecked

它们是完全不同的东西, SelectedItem称为ListBoxItem,您的单选按钮位于ListBoxItem中


您必须为属性IsChecked(RadioButton)进行绑定。

如果您只想将
RadioButton.IsChecked
ListBoxItem.IsSelected
同步,则可以使用绑定

<RadioButton Content="{Binding}" GroupName="group"
             IsChecked="{Binding Path=IsSelected, RelativeSource={
                 RelativeSource AncestorType={x:Type ListBoxItem}},Mode=TwoWay}"/>
如果你想让它被选中,不管这个元素是否还有键盘焦点,你必须使用一点代码隐藏

<Style TargetType="{x:Type ListBoxItem}">
    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>

如果您只想将
单选按钮.IsChecked
ListBoxItem.IsSelected
同步,则可以使用绑定

<RadioButton Content="{Binding}" GroupName="group"
             IsChecked="{Binding Path=IsSelected, RelativeSource={
                 RelativeSource AncestorType={x:Type ListBoxItem}},Mode=TwoWay}"/>
如果你想让它被选中,不管这个元素是否还有键盘焦点,你必须使用一点代码隐藏

<Style TargetType="{x:Type ListBoxItem}">
    <EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>

尝试将
ListBoxItem.ishitestvisible
设置为false(您可以在xaml中执行此操作)。它基本上解决了我的选择问题。我的问题是,仅当我单击列表框行中的空白时,选择才起作用,而不是自定义内容。

请尝试将
ListBoxItem.ishitestvisible
设置为false(您可以在xaml中执行此操作)。它基本上解决了我的选择问题。我的问题是,仅当我单击列表框行中的空白处时,选择才起作用,而不是自定义内容。

我同意,我经常看到这个错误。我同意,我经常看到这个错误。谢谢,这正是我一直在寻找的。回答得好!谢谢,这正是我想要的。回答得好!