不允许在列表框中取消选择/取消选择

发布于 2024-11-03 23:33:03 字数 158 浏览 1 评论 0原文

有没有办法配置 WPF ListBox 使其无法取消选择/取消选择项目?那么总有一个项目被选中?

我的 ListBox 绑定到 ObservableCollection

Is there a way to configure the WPF ListBox such that it's not possible to deselect/unselect an item? So there is always an item selected?

My ListBox binds to an ObservableCollection<MyClass>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2024-11-10 23:33:03

您可以处理 SelectionChanged 事件,并在 SelectedItem 计算结果为 null 时设置选择。要重新选择已取消选择的项目,您可以在私有字段中跟踪最后选择的项目,该字段应始终在 SelectionChanged 事件中更新。

You can handle the SelectionChanged event and set a selection if the SelectedItem evaluates to null. To reselect an item that has been unselected you can keep track of the last selected item in a private field, which should always be updated in the SelectionChanged event.

傲鸠 2024-11-10 23:33:03

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

    <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/

Sound more like a RadioButtonGroup.You can customize the ItemContainerStyle of listbox and easily have this behavior in your listboxes.See below

    <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>

Check the below link for more

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文