WPF - 造型组合框

时间:2010-03-17 11:46:52

标签: wpf combobox styles

我正在尝试在WPF中设置Comboboxes样式,使它们是白色的,并且与TextBoxes具有相同的边框。到目前为止,我有以下样式,但不知道如何设置边框:

<Style TargetType="ComboBox">
    <Setter Property="Margin" Value="0,2,0,2" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Background" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                 ???
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

2 个答案:

答案 0 :(得分:3)

使用Expression Blend。如果您希望ComboBox看起来与TextBox类似,请右键点击TextBox并选择TextBlock来查看Edit Template > Edit a Copy模板并使用该模板修改您的ComboBox {1}}模板!

答案 1 :(得分:2)

在你的controltemplate之间请实现以下内容:

<Grid SnapsToDevicePixels="true">
    <Border
    x:Name="Bd"
    Background="Transparent"
    BorderBrush="#FF888888"
    Padding="1"
    CornerRadius="5" BorderThickness="2,2,2,2">
        <Grid
        Grid.IsSharedSizeScope="true">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" 
                   SharedSizeGroup="ComboBoxButton" />
            </Grid.ColumnDefinitions>
            <Border x:Name="SelectedItemBorder" Grid.ColumnSpan="2" />
            <ContentPresenter
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
              Grid.Column="1"
              Content="{TemplateBinding SelectionBoxItem}"
              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" 
              TextBlock.Foreground="White"
              Height="Auto"
              Margin="2,2,7,2"
              VerticalAlignment="Center" />
            <ToggleButton
              Style="{StaticResource ComboBoxTransparentButtonStyle}"
              Grid.ColumnSpan="3"
              IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, 
                          RelativeSource={RelativeSource TemplatedParent}}"
              Opacity="0.25" />
        </Grid>
    </Border>
    <Popup Focusable="false" AllowsTransparency="true"
      IsOpen="{Binding Path=IsDropDownOpen, 
               RelativeSource={RelativeSource TemplatedParent}}"
      Placement="Bottom" 
      PopupAnimation="{DynamicResource 
                      {x:Static SystemParameters.ComboBoxPopupAnimationKey}}"
      x:Name="PART_Popup">
        <Border CornerRadius="5" x:Name="DropDownBorder"
          MaxHeight="{TemplateBinding MaxDropDownHeight}"
          MinWidth="{TemplateBinding ActualWidth}"
          Background="#FF7E7E7E"
          BorderThickness="1">
            <ScrollViewer Foreground="#FFFFFFFF">
                <ItemsPresenter
                   SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                   KeyboardNavigation.DirectionalNavigation="Contained"
                   TextBlock.Foreground="White"
                   VerticalAlignment="Stretch" />
            </ScrollViewer>
        </Border>
    </Popup>
</Grid>
<ControlTemplate.Triggers>
    <Trigger Property="IsFocused" Value="True">
        <Setter Property="BorderBrush" TargetName="Bd" Value="#FFFFFFFF"/>
    </Trigger>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="BorderBrush" TargetName="Bd" Value="#FFD2ECCF"/>
    </Trigger>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsSelectionBoxHighlighted" Value="true" />
            <Condition Property="IsDropDownOpen" Value="false" />
        </MultiTrigger.Conditions>
        <Setter Property="Foreground" Value="white" />
        <Setter Property="BorderBrush" Value="{x:Null}" />
    </MultiTrigger>
    <Trigger Property="IsSelectionBoxHighlighted" Value="true">
        <Setter Property="Background" TargetName="SelectedItemBorder"
           Value="Transparent" />
    </Trigger>
    <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
    </Trigger>
    <Trigger Property="HasItems" Value="false">
        <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95" />
    </Trigger>
    <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Foreground" 
           Value="{DynamicResource 
                  {x:Static SystemColors.GrayTextBrushKey}}" />
    <Setter Property="Background" TargetName="Bd" 
       Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    </Trigger>
    <Trigger Property="IsGrouping" Value="true">
        <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
    </Trigger>
</ControlTemplate.Triggers>