不允许在ListBox中取消选择/选择。

3
有没有一种方法可以配置WPF ListBox,使其无法取消选择/取消选中项目?因此始终有一个项目被选中?
我的ListBox绑定到ObservableCollection<MyClass>

当一个项目被选择后,你可以禁用你的ListBox。 - IVerzin
2个回答

10

如果SelectedItem的值为null,则可以处理SelectionChanged事件并设置所选内容。要重新选择已取消选择的项,可以在私有字段中跟踪最后选择的项,并应始终在SelectionChanged事件中更新该字段。


也许不是最优雅的解决方案,但对我很有效,谢谢! - Robbert Dam
1
在SelectionChanged事件中,您可以通过SelectionChangedEventArgs中的RemovedItems属性获取已移除的项。因此,您无需跟踪上一个选定的项,并且它也适用于多选列表框。 - Matt

2
听起来更像是一个 RadioButtonGroup。您可以自定义listbox的ItemContainerStyle,并轻松地在listbox中实现此行为。请参见下文。

    <Style TargetType="{x:Type RadioButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type RadioButton}">
                    <Border 
      Name="Border"
      Padding="2"
      SnapsToDevicePixels="true">

                        <Grid
                               VerticalAlignment="Center">
                            <ContentPresenter x:Name="contentPresenter"
                                              TextBlock.FontWeight="Bold"
                                              TextBlock.Foreground="{TemplateBinding Foreground}"
                                              TextBlock.FontSize="10"
                                              HorizontalAlignment="Stretch"
                                              VerticalAlignment="Stretch" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="Border" Property="Background"
                Value="{StaticResource SelectedBackgroundBrush}"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground"
                Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ObjectDataProvider x:Key="WindowStyles" MethodName="GetValues"
    ObjectType="{x:Type sys:Enum}" >
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="WindowStyle" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
        <Setter Property="BorderBrush" Value="{x:Null}" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Margin" Value="6,2" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border Background="Transparent">
                                    <RadioButton Focusable="False"
                    IsHitTestVisible="False"
                    IsChecked="{TemplateBinding IsSelected}">
                                        <ContentPresenter />
                                    </RadioButton>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>

        <StackPanel Margin="10">
            <TextBlock FontWeight="Bold">WindowStyle:</TextBlock>
            <ListBox Name="WindowStyleSelector" SelectedIndex="1" Margin="10,0,0,0"
    Style="{StaticResource RadioButtonList}" Tag="Horizontal"
    ItemsSource="{Binding Source={StaticResource WindowStyles}}" />
        </StackPanel>
</Grid>

请查看下面的链接以获取更多信息。

http://drwpf.com/blog/2009/05/12/itemscontrol-l-is-for-lookless/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接