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

时间:2011-04-28 08:34:41

标签: c# .net wpf xaml

有没有办法配置WPF ListBox,以便取消选择/取消选择项目?所以总有一个项目被选中了?

我的ListBox绑定到ObservableCollection<MyClass>

2 个答案:

答案 0 :(得分:10)

如果SelectionChanged评估为SelectedItem,您可以处理null事件并设置选择。要重新选择未被选中的项目,您可以跟踪私有字段中最后选择的项目,该项目应始终在SelectionChanged事件中更新。

答案 1 :(得分:2)

听起来更像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/