带有嵌入式CheckBox的ListBox限制要检查的一个项目

时间:2011-09-19 19:10:00

标签: wpf checkbox listbox

我有一个ListBox,其中包含一个由包含CheckBox和Label的StackPanel组成的ItemTemplate。我想一次只允许检查一个列表项。我无法理解如何实现这一目标。这是描述列表框的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 class Player
{
    public string Name { get; set; }
    public bool IsDefault { get; set; }
}

我的Players列表的定义如下:

public ObservableCollection<Player> Players { get; private set; }

我的SelectedPlayer定义如下:

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); }
}

我找不到可以帮助我的帖子或问题。我已经使用了CheckBox的Checked事件处理程序,但我似乎无法理解如何将列表与列表中的正确Player相关联,因为ListBox不会调整{选中或取消选中CheckBox时的{1}}。

非常感谢。

3 个答案:

答案 0 :(得分:1)

我通常使用以下样式来使用CheckBoxes显示ListBox(只需将CheckBox控件替换为RadioButton

,您也可以使用RadioButtons
<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}">

答案 1 :(得分:0)

尝试将ListBox配置为选择模式单一。而不是单独的标签,您可以只将Name绑定到CheckBox的Content属性。我喜欢空间的方式。您可能需要在播放器上实现InotifyPropertyChanged。

答案 2 :(得分:0)

如果您的某些内容应该与RadioButton相似,为什么不使用RadioButton