Search code examples
wpfcontrolsvisual-studio-2022

Combobox color changes are not displayed in wpf designer


When I set the background color of the combobox or the border, the changes are not shown in the designer, but at runtime it is displayed correctly .Changes are not shown from the code or properties window or anywhere in the application. This only happens with the combobox control; the others controls are displayed correctly.

I even reset VS2022 but it doesn't work. enter image description here

<Window
x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="232"
Height="114"
mc:Ignorable="d">
<Grid>
    <ComboBox HorizontalAlignment="Left" Margin="69,35,0,0" VerticalAlignment="Top" Width="120" Background="#0720F3"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="100,65,0,0" VerticalAlignment="Top" Background="#FFD01A1A"/>
</Grid>

I have this problem in all wpf projects.


Solution

  • It is pretty important to understand all the parts that make up a XAML control.

    There are multiple parts to a combobox that get set during run-time, to see combo-box templates and its expected components, refer to https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/combobox-styles-and-templates?view=netframeworkdesktop-4.8

    When you set Combobox.Background, it changes the template background, however some elements of the Combobox do not use the templated background property.

    I believe in this case the PART_EditableTextBox has its own background property. If you did not set your combobox to be editable, this textbox wont appear during runtime. It may be covering the actual background of the combobox during designtime, however.

    You can create your own combobox style that overwrites the default style for all of the template parts of a combobox, however, this may, or may not be overkill.