簡體   English   中英

WPF-樣式組合框

[英]WPF - styling comboboxes

我正在嘗試在WPF中設置組合框樣式,以使其為白色,並且具有與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>

使用表達式混合。 如果您希望ComboBox看起來與TextBox相似,請右鍵單擊TextBlock並選擇“ Edit Template > Edit a Copy然后使用它來修改ComboBox模板,以查看TextBox模板!

在您的控件模板之間,請實現以下內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM