2010-03-17 27 views
0

我想在WPF中设置组合框的样式,以便它们是白色的,并且具有与TextBox相同的边框。我有以下的风格,到目前为止,但不知道如何设置边框:WPF - 造型组合框

<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

使用Expression Blend中。如果你想ComboBox看起来类似于TextBox,看看TextBox模板右键单击TextBlock并选择Edit Template > Edit a Copy并使用它来修改您的ComboBox模板!

1

在您的控件模板之间请执行这样的事情:

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