且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF更改代码中的edittable组合框的背景颜色

更新时间:2023-10-03 10:24:10

Changing the combobox's background using the background property only use to work in Win7 and older, in windows 8 and above the default template for the ComboBox has been changed, to fix that you should edit the default template,

  1. using VisualStudio 2013 or Blend, Right Click the combobox and choose EditTemplate > Edit a Copy :

  1. In the generated Xaml search for <ControlTemplate TargetType="{x:Type ToggleButton}"> and replace the {StaticResource ComboBox.Static.Background} markup with a TemplateBinding to the Background property, your code should look like this after the update :

     ...
      <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border x:Name="templateRoot" BorderBrush="{StaticResource ComboBox.Static.Border}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" 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>
                    <ControlTemplate.Triggers>
                        <MultiDataTrigger>
                         ...
    

  2. Now, you can use the Background property to change the Combobox color:

    <Grid>
      <ComboBox IsEditable="True" x:Name="EditableComboBox" Background="PeachPuff" VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Style="{DynamicResource ComboBoxStyle1}" >
      </ComboBox>
    </Grid>
    

相关阅读

推荐文章