【问题标题】:How to change comboboxes background如何更改组合框背景
【发布时间】:2016-04-14 12:37:57
【问题描述】:

如何更改组合框背景 但不适用于组合框本身,因为画笔属性不适用 i want to change this

组合框:

<ComboBox x:Name="Bil_box" HorizontalAlignment="Center" Margin="57,33,968,799" VerticalAlignment="Center" Width="151" Height="40" Foreground="#FF00FB0B" Background="#FF303030" ItemTemplate="{StaticResource cmbTemplate}">
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF303030" />
    </ComboBox.Resources>
</ComboBox>

模板:

<DataTemplate x:Key="cmbTemplate">
    <WrapPanel Margin="0 5 0 5" Height="30">
        <Image Width="20" Height="20" Stretch="Fill" Source="{Binding Billed}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/>
        <Label Content="{Binding bilmærke}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="12" Foreground="#FF00FB0B"/>
        <Label Content="{Binding Krydsmål}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="12" Foreground="#FF00FB0B"/>
    </WrapPanel>
</DataTemplate>`

【问题讨论】:

    标签: c# wpf xaml combobox datatemplate


    【解决方案1】:

    您要查找的项目是一个名为 templateRootBorder 控件

    您首先必须为您的 ComboBox 编辑一个模板(如果您还没有模板,请编辑一个副本)。然后查找名为toggleButtonToggleButton 控件。

    右击并编辑此模板(编辑当前),您应该能够找到我上面提到的边框。更改 Background 即可。

    代码太长,无法发布(由于模板的大小),但这是相关位的 sn-p(我已将背景更改为 红色

    <Style x:Key="ComboBoxToggleButton" 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}">
                        <Border x:Name="templateRoot" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" Background="Red" SnapsToDevicePixels="true">
                            <Border x:Name="splitBorder" BorderBrush="Transparent" BorderThickness="1" HorizontalAlignment="Right" Margin="0" SnapsToDevicePixels="true" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                                <Path x:Name="arrow" Data="F1 M 0,0 L 2.667,2.66665 L 5.3334,0 L 5.3334,-1.78168 L 2.6667,0.88501 L0,-1.78168 L0,0 Z" Fill="{StaticResource ComboBox.Static.Glyph}" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
                            </Border>
                        </Border>
    

    【讨论】:

    • 有没有其他方法可以做到这一点,因为你只能在没有 PresentationFramework.Aero2 的情况下做到这一点,这在 win 7 上给了我一些错误,因为它只是一个 win8 及更高版本的功能