Jason Hu
Jason Hu

Reputation: 6333

how can i set the background color of the TextBox in an editable ComboBox?

enter image description here

The image shown is a part of my wpf application. i set it to be a editable combobox to allow use input. however, the TextBox inside has a white background, which i don't like a lot. how could i change the color of it to a different color?(in this case, i want to change it to "#FF2E2E2E".)

my xaml:

<ComboBox Height="23" Background="#FF2E2E2E" Foreground="#FF979797" Grid.Column="1" Margin="107,43,0,0" HorizontalAlignment="Left" Width="133" IsEditable="True" VerticalAlignment="Top" IsEnabled="False">
        <ComboBox.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF232323" />
        </ComboBox.Resources>
    </ComboBox>

UPDATE:

enter image description here

it has nothing to do with other controls and resources.

Upvotes: 0

Views: 3805

Answers (2)

Bj&#246;rn
Bj&#246;rn

Reputation: 3418

I agree with King King: I made a new WPF window and added most of your code for the combobox (I didn't set IsEnabled=false) and it looks like this for me:
combobox

This is the entire code for the window:

<Window x:Class="WpfApplication35.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Background="#FF2E2E2E" 
                  IsEditable="True" 
                  VerticalAlignment="Top" 
                  HorizontalAlignment="Left" 
                  Width="100">
            <ComboBox.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" 
                                 Color="#FF2E2E2E" />
            </ComboBox.Resources>
        </ComboBox>
    </Grid>
</Window>

Maybe you just need to do a "clean" on your project?

Upvotes: 0

Jason Hu
Jason Hu

Reputation: 6333

fine... finally found out myself.

we can change it by modifying the default template:

<SolidColorBrush x:Key="TextBox.Static.Background" Color="#FF2E2E2E"/>
<ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
                        <Grid x:Name="templateRoot" SnapsToDevicePixels="true">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/>
                            </Grid.ColumnDefinitions>
                            <Popup x:Name="PART_Popup" AllowsTransparency="true" Grid.ColumnSpan="2" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">
                                <Themes:SystemDropShadowChrome x:Name="shadow" Color="Transparent" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=templateRoot}">
                                    <Border x:Name="dropDownBorder" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}">
                                        <ScrollViewer x:Name="DropDownScrollViewer">
                                            <Grid x:Name="grid" RenderOptions.ClearTypeHint="Enabled">
                                                <Canvas x:Name="canvas" HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
                                                    <Rectangle x:Name="opaqueRect" Fill="{Binding Background, ElementName=dropDownBorder}" Height="{Binding ActualHeight, ElementName=dropDownBorder}" Width="{Binding ActualWidth, ElementName=dropDownBorder}"/>
                                                </Canvas>
                                                <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                            </Grid>
                                        </ScrollViewer>
                                    </Border>
                                </Themes:SystemDropShadowChrome>
                            </Popup>
                            <ToggleButton x:Name="toggleButton" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Style="{StaticResource ComboBoxToggleButton}"/>
                            <Border x:Name="border" Background="{StaticResource TextBox.Static.Background}" Margin="{TemplateBinding BorderThickness}">
                                <TextBox x:Name="PART_EditableTextBox" Background="{StaticResource TextBox.Static.Background}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocusWithin" Value="true">
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                                <Setter Property="Margin" TargetName="shadow" Value="0,0,5,5"/>
                                <Setter Property="Color" TargetName="shadow" Value="#71000000"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="false">
                                <Setter Property="Height" TargetName="dropDownBorder" Value="95"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsGrouping" Value="true"/>
                                    <Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                            </MultiTrigger>
                            <Trigger Property="ScrollViewer.CanContentScroll" SourceName="DropDownScrollViewer" Value="false">
                                <Setter Property="Canvas.Top" TargetName="opaqueRect" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}"/>
                                <Setter Property="Canvas.Left" TargetName="opaqueRect" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>

i delete some irrelevant code btw. the key point is

<SolidColorBrush x:Key="TextBox.Static.Background" Color="#FF2E2E2E"/>

and

                        <Border x:Name="border" Background="{StaticResource TextBox.Static.Background}" Margin="{TemplateBinding BorderThickness}">

all i have to do is modify the color of the Border around the textbox underlying!!!

Upvotes: 2

Related Questions