更改组合框的颜色(显示箭头按钮的组合框的标题框)

时间:2014-03-26 06:37:10

标签: wpf xaml

我想在组合框中显示组合框的标题框(显示箭头按钮的组合框的标题框)永久改变组合框的标题,如何做到这一点尝试了很多东西,但它不是那样的我想要的是什么

5 个答案:

答案 0 :(得分:1)

这很简单。您只需添加资源并消耗资源。

试试这个:

    <Window.Resources>
        <Style x:Key="stylecombo" TargetType="ComboBox">
            <Setter Property="ComboBox.Background"
                    Value="Beige" />
        </Style>
    </Window.Resources>

    <ComboBox Style="{StaticResource stylecombo}" Height="25" Width="100" Margin="23,22,285,262" IsEditable="True" Text="Your Choice...">
            <ComboBoxItem Content="1"></ComboBoxItem>
            <ComboBoxItem Content="2"></ComboBoxItem>
    </ComboBox>

如果您使用的是usercontrol,请使用UserControl.Resources而不是Window.Resources。

希望这会对你有所帮助。!! :)

答案 1 :(得分:1)

这是重新组合组合框控件或获取控件的TemplatedChild元素的问题。

第一个是我自己使用最多的那个,所以我在整个应用程序中都有一些一致的东西。

ComboBox ResourceDictionary

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Resources/Shared.xaml" />
</ResourceDictionary.MergedDictionaries>

<!-- SimpleStyles: ComboBox -->
<ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="40" />
        </Grid.ColumnDefinitions>

    x:Name="Border" 
    Grid.ColumnSpan="2"
    CornerRadius="2"
    Background="{StaticResource ComboBoxButtonBackgroundBrush}"
    BorderBrush="{StaticResource NormalBorderBrush}"
    BorderThickness="1" />
        <!--Border for colonne 1 i ComboBoxen-->
        <Border 
    Grid.Column="0"
    CornerRadius="0" 
    Margin="1" 
    Background="{StaticResource ComboBoxBackgroundBrush}" 
    BorderBrush="{StaticResource NormalBorderBrush}"
    BorderThickness="0,0,1,0" />
        <Path 
    x:Name="Arrow"
    Grid.Column="1"     
    Fill="{StaticResource GlyphBrush}"
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Data="M -2 -1 L 6 2 L -2 6"/> 
    </Grid>
    <!-- 
        Consider path data as a grid with cordinates.
        It makes the drawing logic so much easier.
         M = starts at (x, y)
         L = Line, drawing line from last point (x, y)
         H = Horizontal line from last point to new point (x)
         V = Vertical line from last point to new point (y)
         C = Curves (x¹, y¹, x², y², x, y) advanced math
         S = Curves (x², y², x, y) advanced math
         Q = Curves (x¹, y¹, x, y) advanced math
         T = Curves (x,y) advanced math
    -->
    <ControlTemplate.Triggers>
        <Trigger Property="ToggleButton.IsMouseOver" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
        </Trigger>
        <Trigger Property="ToggleButton.IsChecked" Value="true">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
            <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource DisabledForegroundBrush}" />
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
    <Border x:Name="PART_ContentHost" Focusable="False" Background="{TemplateBinding Background}" />
</ControlTemplate>

<Style x:Key="{x:Type ComboBox}" TargetType="ComboBox">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>

          <ToggleButton 
          Name="ToggleButton" 
          Template="{StaticResource ComboBoxToggleButton}" 
          Grid.Column="2" 
          Focusable="false"
          IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
          ClickMode="Press">
          </ToggleButton>

          <ContentPresenter
          Name="ContentSite"
          IsHitTestVisible="False" 
          Content="{TemplateBinding SelectionBoxItem}"
          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
          Margin="3,3,23,3"
          VerticalAlignment="Center"
          HorizontalAlignment="Left" 
          />

          <TextBox x:Name="PART_EditableTextBox"
          Style="{x:Null}" 
          Template="{StaticResource ComboBoxTextBox}" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Center" 
          Margin="3,3,23,3"
          Focusable="True" 
          Background="Transparent"
          Visibility="Hidden"
          IsReadOnly="{TemplateBinding IsReadOnly}"
          />

          <Popup 
          Name="Popup"
          Placement="Bottom"
          IsOpen="{TemplateBinding IsDropDownOpen}"
          AllowsTransparency="True" 
          Focusable="False"
          PopupAnimation="Slide"
          >

                  

            <Grid 
            Name="DropDown"
            SnapsToDevicePixels="True"                
            MinWidth="{TemplateBinding ActualWidth}"
            MaxHeight="{TemplateBinding MaxDropDownHeight}"
            >

              <Border 
              x:Name="DropDownBorder"
              Background="{StaticResource WindowBackgroundBrush}"
              BorderThickness="1"
              BorderBrush="{StaticResource SolidBorderBrush}"/>
                            <ScrollViewer Margin="4,6,4,6" SnapsToDevicePixels="True">
                                <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />

                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasItems" Value="false">
                        <Setter TargetName="DropDownBorder" Property="MinHeight" Value="95"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsGrouping" Value="true">
                        <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                    </Trigger>
                    <Trigger SourceName="Popup" Property="Popup.AllowsTransparency" Value="true">
                        <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="DropDownBorder" Property="Margin" Value="0,0,0,0"/> 
                        <!-- Her sættes der margin og corner radius på dropdownlisten. -->
                    </Trigger>
                    <Trigger Property="IsEditable"
             Value="true">
                        <Setter Property="IsTabStop" Value="false"/>
                        <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                        <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

<!-- SimpleStyles: ComboBoxItem -->
<Style x:Key="{x:Type ComboBoxItem}" TargetType="ComboBoxItem">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBoxItem">
                <Border 
        Name="Border"
        Padding="2"
        SnapsToDevicePixels="true">
                    <ContentPresenter />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsHighlighted" Value="true">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource SelectedBackgroundBrush}"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是原始模板的示例,稍作修改。在这里,您可以在控件中更改您喜欢的任何内容,因为所有模板化的孩子都在那里。

答案 2 :(得分:0)

  <Window.Resources>
        <Color x:Key="GrayColor_">#FF928B81</Color>
        <Color x:Key="LightGrayColor_">#FFC3C3C3</Color>
        <Color x:Key="LightLightGrayColor_">#FFF1F1F1</Color>
        <SolidColorBrush x:Key="GrayColor" Color="{StaticResource GrayColor_}"/>
        <SolidColorBrush x:Key="LightGrayColor" Color="{StaticResource LightGrayColor_}"/>
        <SolidColorBrush x:Key="LightLightGrayColor" Color="{StaticResource LightLightGrayColor_}"/>

        <Color x:Key="BlueColor_">#0073b0</Color>
        <Color x:Key="DarkBlueColor_">#FF004165</Color>
        <Color x:Key="LightBlueColor_">#FFa4ddfa</Color>
        <SolidColorBrush x:Key="BlueColor" Color="{StaticResource BlueColor_}" />
        <SolidColorBrush x:Key="DarkBlueColor" Color="{StaticResource DarkBlueColor_}" />
        <SolidColorBrush x:Key="LightBlueColor" Color="{StaticResource LightBlueColor_}" />

        <SolidColorBrush x:Key="Foreground" Color="Black"/>
        <SolidColorBrush x:Key="ForegroundWhite" Color="White"/>


        <Style x:Key="LightGrayBox" TargetType="{x:Type Border}">
            <Setter Property="Background" Value="White"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="BorderBrush" Value="{StaticResource LightGrayColor}" />
        </Style>


        <Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="20"/>
                            </Grid.ColumnDefinitions>
                            <Border x:Name="Border" Style="{DynamicResource LightGrayBox}" Grid.ColumnSpan="2" />
                            <Path x:Name="Arrow" Grid.Column="1" Opacity="0.6" Fill="{StaticResource GrayColor}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                                <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                                <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource BlueColor}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource BlueColor}"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                                <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource BlueColor}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ComboBoxToggleButtonActive" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="20"/>
                            </Grid.ColumnDefinitions>
                            <Border x:Name="Border" Grid.ColumnSpan="2" Background="{DynamicResource BlueColor}" />
                            <Path x:Name="Arrow" Grid.Column="1" Opacity="1" Fill="White" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="true">
                                <Setter TargetName="Border" Property="Background" Value="{DynamicResource BlueColor}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="White"/>
                                <Setter Property="Foreground" Value="White" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ControlTemplate x:Key="ComboBoxTextBox" TargetType="TextBox">
            <Border x:Name="PART_ContentHost" Background="{TemplateBinding Background}" />
        </ControlTemplate>

        <Style x:Key="StandardComboBox" TargetType="{x:Type ComboBox}">
            <Setter Property="Foreground" Value="{StaticResource Foreground}"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
            <Setter Property="IsEditable" Value="True" />
            <Setter Property="OverridesDefaultStyle" Value="true"/>
            <Setter Property="SnapsToDevicePixels" Value="true"/>
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
            <Setter Property="VerticalAlignment" Value="Top"/>
            <Setter Property="Width" Value="120"/>
            <Setter Property="IsSelected" Value="{Binding IsKeyboardFocusWithin, RelativeSource={RelativeSource Self}, Mode=OneWay}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <ToggleButton Name="ToggleButton" Style="{StaticResource ComboBoxToggleButton}" Grid.Column="2" Focusable="false" ClickMode="Press"
                                      IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
                            <Label Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                               ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />
                            <!--<ContentPresenter Name="ContentSite" IsHitTestVisible="False" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" 
                                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="3,3,23,3" VerticalAlignment="Center" HorizontalAlignment="Left" />-->
                            <TextBox x:Name="PART_EditableTextBox" 
                                 CaretBrush="{DynamicResource ForegroundWhite}"
                                 Style="{x:Null}" 
                                 Template="{StaticResource ComboBoxTextBox}" 
                                 HorizontalAlignment="Left" 
                                 VerticalAlignment="Center" 
                                 Margin="3,3,23,3" 
                                 Focusable="True" 
                                 Background="Transparent" 
                                 Foreground="{StaticResource ForegroundWhite}" 
                                 Visibility="Hidden"
                                 IsReadOnly="{TemplateBinding IsReadOnly}"/>
                            <Popup VerticalOffset="-1" SnapsToDevicePixels="True" Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"  Focusable="False" PopupAnimation="Fade">
                                <Grid Name="DropDown" SnapsToDevicePixels="True" MinWidth="{TemplateBinding ActualWidth}" MaxHeight="200">
                                    <Border x:Name="DropDownBorder" Style="{DynamicResource LightGrayBox}"/>
                                    <ScrollViewer SnapsToDevicePixels="True">
                                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasItems" Value="false">
                                <Setter TargetName="DropDownBorder" Property="MinHeight" Value="20"/>
                            </Trigger>
                            <Trigger Property="IsGrouping" Value="true">
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="ToggleButton" Property="Style" Value="{StaticResource ComboBoxToggleButtonActive}" />
                                <Setter TargetName="ContentSite" Property="Foreground" Value="{DynamicResource ForegroundWhite}"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="PART_EditableTextBox" Property="Foreground" Value="{DynamicResource Foreground}"/>
                                <Setter TargetName="ContentSite" Property="Foreground" Value="{DynamicResource Foreground}"/>
                            </Trigger>
                            <Trigger Property="IsEditable" Value="true">
                                <Setter Property="IsTabStop" Value="false"/>
                                <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
                                <Setter TargetName="ContentSite" Property="Visibility" Value="Hidden"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Resources>
                <Style TargetType="ComboBoxItem">
                    <Setter Property="SnapsToDevicePixels" Value="true"/>
                    <Setter Property="OverridesDefaultStyle" Value="true"/>
                    <Setter Property="HorizontalContentAlignment" Value="Left" />
                    <Setter Property="VerticalContentAlignment" Value="Center" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="ComboBoxItem">
                                <Border Name="Border" Padding="2" SnapsToDevicePixels="true" BorderThickness="1">
                                    <ContentPresenter />
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsHighlighted" Value="true">
                                        <Setter TargetName="Border" Property="Background" Value="{DynamicResource BlueColor}"/>
                                        <Setter TargetName="Border" Property="Padding" Value="2,3,2,3" />
                                        <Setter Property="Foreground" Value="White" />
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>

                </Style>

            </Style.Resources>
        </Style>
    </Window.Resources>


    <Grid>
        <ComboBox Height="23" Margin="122,32,100,0" Style="{StaticResource StandardComboBox}"  Name="comboBox1" VerticalAlignment="Top">

        </ComboBox>
    </Grid>

答案 3 :(得分:0)

<Style x:Key="ComboBoxToggleButton" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="20"/>
                            </Grid.ColumnDefinitions>
                            **<Border x:Name="Border" Style="{DynamicResource LightGrayBox}" Grid.ColumnSpan="2" Background="{StaticResource BlueColor}" />**
                            <Path x:Name="Arrow" Grid.Column="1" Opacity="0.6" Fill="{StaticResource GrayColor}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                                <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                                <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource BlueColor}" />
                                <Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource BlueColor}"/>
                            </Trigger>
                            <Trigger Property="ToggleButton.IsChecked" Value="true">
                                <Setter TargetName="Arrow" Property="Opacity" Value="1" />
                                <Setter TargetName="Arrow" Property="Fill" Value="{StaticResource BlueColor}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

在代码中包含BOLD行。

答案 4 :(得分:0)

组合框的模板说它的背景是透明的,只需在它后面添加一个面板/网格,并将面板/网格的颜色更改为所需的颜色,它将变为该颜色。

您必须创建一个网格,设置其背景颜色,将其带到组合框控件上,设置其高度和宽度以匹配组合框,然后右键单击组合框,转到订单,选择带到前面

这样就可以了。 与我合作:)

相关问题