Wpf 带有嵌入式复选框的列表框,限制选中一项

Wpf 带有嵌入式复选框的列表框,限制选中一项,wpf,checkbox,listbox,Wpf,Checkbox,Listbox,我有一个列表框,其中包含一个ItemTemplate,它由一个StackPanel组成,其中包含一个复选框和一个标签。我希望一次只检查一个列表项。我很难理解如何才能完成这项任务。下面是描述列表框的XAML: <ListBox Grid.Row="0" Grid.Column="0" Width="180" HorizontalAlignment="Left" x:Name="listboxPlayers"

我有一个列表框,其中包含一个ItemTemplate,它由一个StackPanel组成,其中包含一个复选框和一个标签。我希望一次只检查一个列表项。我很难理解如何才能完成这项任务。下面是描述列表框的XAML:

<ListBox Grid.Row="0"
         Grid.Column="0"
         Width="180"
         HorizontalAlignment="Left"
         x:Name="listboxPlayers"
         ItemsSource="{Binding Players}"
         SelectedItem="{Binding SelectedPlayer, Mode=TwoWay}"
         ScrollViewer.VerticalScrollBarVisibility="Visible">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="{Binding IsDefault, Mode=TwoWay}"
                          VerticalAlignment="Center"
                          Checked="CheckBox_Checked"
                          Unchecked="CheckBox_Unchecked"/>
                <Label Content="{Binding Name}"
                       VerticalAlignment="Center" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
我的
玩家名单定义如下:

public ObservableCollection<Player> Players { get; private set; }
public static readonly DependencyProperty SelectedPlayerProperty =
    DependencyProperty.Register("SelectedPlayer", typeof(Player), typeof(MainWindow),
        new FrameworkPropertyMetadata());

public Player SelectedPlayer
{
    get { return (Player)GetValue(SelectedPlayerProperty); }
    set { SetValue(SelectedPlayerProperty, value); }
}
我还没有找到一个能帮助我的帖子或问题。我已经尝试过为复选框使用选中的事件处理程序,但我似乎不知道如何将列表与列表中正确的
播放器
关联,因为选中或取消选中复选框时,列表框不会调整
SelectedPlayer


非常感谢。

尝试将列表框配置为单选模式。您可以将名称绑定到复选框的内容属性,而不是单独的标签。我喜欢这种方式。您可能需要在播放器上实现InotifyPropertyChanged。

如果您有类似的功能,为什么不使用单选按钮?

我通常使用以下样式来使用复选框显示列表框(您也可以使用单选按钮,只需将
复选框
控件替换为
单选按钮

<Style x:Key="CheckBoxListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="BorderBrush" Value="Transparent"/>
    <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Cycle" />
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="{x:Type ListBoxItem}" >
                <Setter Property="Margin" Value="2, 2, 2, 0" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border Background="Transparent">
                                <CheckBox IsHitTestVisible="False" Focusable="false" 
                                    Content="{TemplateBinding ContentPresenter.Content}"  
                                    IsChecked="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

然后,它被类似的东西简单地使用

<ListBox ItemsSource="{Binding Players}"
         Style="{StaticResource CheckBoxListBoxStyle}"
         SelectedItem="{Binding SelectedPlayer, Mode=TwoWay}">


我可以使用动态添加的单选按钮吗?可以,为什么不可以?只需确保分配一个通用的
组名即可。
<ListBox ItemsSource="{Binding Players}"
         Style="{StaticResource CheckBoxListBoxStyle}"
         SelectedItem="{Binding SelectedPlayer, Mode=TwoWay}">