2012-07-10 123 views
1

我想要一个或两个复选框在我的组合框(如微软字)enter image description here添加复选框到ComboboxItem

我把我的复选框放在一个单独的Combobox项目中,并将它们设置为禁用项目。 (我不希望用户可以选择这个项目)。但该复选框始终处于禁用状态。 enter image description here

有没有另一种解决方案?

这是我的代码:

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

回答

1

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

话虽如此,如果你坚持一个组合框,添加列表之外的项目,你可以修改的模板组合框(使用混合为您生成一个副本):

<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。如果您不混合复制,试试上面的模板,你还需要下面的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> 
+0

@John_Bowen谢谢,但是有没有简单的方法? – David 2012-07-11 14:53:39

+1

你正在试图让ComboBox做一些复杂的事情,它不是为了支持而设计的,所以没有一个你可以直接翻转的开关。编辑模板并不困难,特别是如果你利用Blend,但如果你不想这样做,你应该尝试一个Menu,它是为这样的事情设计的。 – 2012-07-11 15:18:32