1

我想在我的组合框中添加一个或两个复选框(如 microsoft word)在此处输入图像描述

我把我的复选框放在一个单独的组合框项目中,并将它们设置为禁用。(我不希望用户可以选择此项目)。但是该复选框始终处于禁用状态。 在此处输入图像描述

还有其他解决方案吗?

那是我的代码:

    <ComboBox Height="36">
        <ComboBoxItem Height="36">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16" />
                        <RowDefinition Height="16" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Item Title 1" Grid.Row="0" FontWeight="Bold" />
                    <TextBlock Text="Item Description 1" Grid.Row="1" FontStyle="Italic" />
                </Grid>
            </Grid>
        </ComboBoxItem>
        <ComboBoxItem Height="36">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="16" />
                        <RowDefinition Height="16" />
                    </Grid.RowDefinitions>
                    <TextBlock Text="Item Title 2" Grid.Row="0" FontWeight="Bold" />
                    <TextBlock Text="Item Description 2" Grid.Row="1" FontStyle="Italic" />
                </Grid>
            </Grid>
        </ComboBoxItem>
        <ComboBoxItem Height="25" IsEnabled="False">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="1">
                    <CheckBox IsChecked="false" HorizontalAlignment="Left" Width="16" Height="16" IsEnabled="True" />
                    <Label Margin="10 0 0 0" HorizontalAlignment="Left" Content="I'm a checkbox ;)" />
                </Grid>
            </Grid>
        </ComboBoxItem>
    </ComboBox>
4

1 回答 1

1

当你禁用一个元素时,你也在禁用它作为一个容器,因此也禁用了它的所有子元素。如果您想要一个未禁用但也不可选择的项目,则不应将其包含在 ComboBox 的项目列表中。出于您的目的,使用菜单可能是一个更好的主意,因为这是您尝试复制的内容,而不是尝试将意外功能挤入 ComboBox。

话虽如此,如果您坚持使用 ComboBox,要在列表之外添加项目,您可以修改 ComboBox 的模板(使用 Blend 为您生成副本):

<ComboBox Height="36">
    <ComboBox.Template>
        <ControlTemplate TargetType="{x:Type ComboBox}">
            <Grid x:Name="MainGrid" SnapsToDevicePixels="true">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                </Grid.ColumnDefinitions>
                <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                    <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}">
                        <Border x:Name="DropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                            <ScrollViewer x:Name="DropDownScrollViewer">
                                <Grid RenderOptions.ClearTypeHint="Enabled">
                                    <Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                        <Rectangle x:Name="OpaqueRect" Fill="{Binding Background, ElementName=DropDownBorder}" Height="{Binding ActualHeight, ElementName=DropDownBorder}" Width="{Binding ActualWidth, ElementName=DropDownBorder}"/>
                                    </Canvas>
                                    <StackPanel>
                                        <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                        <CheckBox IsChecked="false" Content="I'm a checkbox ;)" Margin="10"/>
                                    </StackPanel>
                                </Grid>
                            </ScrollViewer>
                        </Border>
                    </Microsoft_Windows_Themes:SystemDropShadowChrome>
                </Popup>
                <ToggleButton BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
                <ContentPresenter ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="false" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                    <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                    <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
                </Trigger>
                <Trigger Property="HasItems" Value="false">
                    <Setter Property="Height" TargetName="DropDownBorder" Value="95"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    <Setter Property="Background" Value="#FFF4F4F4"/>
                </Trigger>
                <Trigger Property="IsGrouping" Value="true">
                    <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                </Trigger>
                <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
                    <Setter Property="Canvas.Top" TargetName="OpaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                    <Setter Property="Canvas.Left" TargetName="OpaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </ComboBox.Template>
    <ComboBoxItem Height="36">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="16" />
                    <RowDefinition Height="16" />
                </Grid.RowDefinitions>
                <TextBlock Text="Item Title 1" Grid.Row="0" FontWeight="Bold" />
                <TextBlock Text="Item Description 1" Grid.Row="1" FontStyle="Italic" />
            </Grid>
        </Grid>
    </ComboBoxItem>
    <ComboBoxItem Height="36">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="30"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="16" />
                    <RowDefinition Height="16" />
                </Grid.RowDefinitions>
                <TextBlock Text="Item Title 2" Grid.Row="0" FontWeight="Bold" />
                <TextBlock Text="Item Description 2" Grid.Row="1" FontStyle="Italic" />
            </Grid>
        </Grid>
    </ComboBoxItem>
</ComboBox>

在这里,我保留了整个默认模板,但在 ItemsPresenter 周围添加了一个额外的 StackPanel,并将新的 CheckBox 放在那里。如果您不使用 Blend 进行复制,要尝试上述模板,您还需要以下 xmlns(以及项目中的 PresentationFramework.Aero 参考):

xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"

和这些资源:

<Window.Resources>
    <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry>
    <Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="IsTabStop" Value="false"/>
        <Setter Property="Focusable" Value="false"/>
        <Setter Property="ClickMode" Value="Press"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true">
                        <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                            <Path x:Name="Arrow" Data="{StaticResource DownArrowGeometry}" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/>
                        </Grid>
                    </Microsoft_Windows_Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
于 2012-07-10T14:44:34.417 回答