Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style Bug? Can not set Background Color within Style in Windows10

when i try to set the BackgroundColor for a ComboBox it's works fine in Windows7 but not in Windows 10. Is this the intended behavior or a bug? EDIT: i dont mean the ComboBox Popup, just the Background Color of the plain ComboBox.

<ComboBox IsEditable="True">
        <ComboBox.Style>
            <Style TargetType="ComboBox">
            <Setter Property="Background" Value="Yellow"></Setter>                 
            </Style>
        </ComboBox.Style>            
  </ComboBox>

EDIT: just to get all the comments right, when i use this code:

 <ComboBox IsEditable="True" Background="Yellow"/>

All work fine and the combobox is yellow. But when i wanna do the same within a Style - i should expect that this should not work, because its intended? Seriously. This is what Styles a for!

Windows 10

Windows 7

like image 571
blindmeis Avatar asked May 10 '19 12:05

blindmeis


People also ask

How do I change the background color in Windows 10?

Select Start > Settings > Personalization > Colors, and then choose your own color, or let Windows pull an accent color from your background.


1 Answers

If you edit the default template:

EditACopy

You'll find the ToggleButton you're referring to is composed of a Border (set through the ControlTemplate) with a hardcoded LinearGradientBrush, that can only be altered by the ControlTemplate's triggers.

<ControlTemplate x:Key="ComboBoxControlTemplate" 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, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" Margin="1" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Placement="Bottom">

            <!--Left out for brevity-->

        </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}}">
            <ToggleButton.Style>
                <Style 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="#FFACACAC" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                                    <Border.Background>
                                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                <GradientStop Color="#FFF0F0F0" Offset="0"/>
                                                <GradientStop Color="#FFE5E5E5" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Border.Background>
                                    <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="F1M0,0L2.667,2.66665 5.3334,0 5.3334,-1.78168 2.6667,0.88501 0,-1.78168 0,0z" Fill="#FF606060" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
                                    </Border>
                                </Border>
                                <ControlTemplate.Triggers>

                                    <!--Left out for brevity-->

                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ToggleButton.Style>
        </ToggleButton>
        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" IsHitTestVisible="False" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
    </Grid>
    <ControlTemplate.Triggers>

        <!--Left out for brevity-->

    </ControlTemplate.Triggers>
</ControlTemplate>

To enable styling, you could forward the BorderBrush and Background properties to the Border through TemplateBinding.

<ControlTemplate TargetType="{x:Type ToggleButton}">
    <Border x:Name="templateRoot" 
            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" 
            Background="{TemplateBinding Background}" SnapsToDevicePixels="True">

        <!--
        <Border.Background>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFF0F0F0" Offset="0"/>
                <GradientStop Color="#FFE5E5E5" Offset="1"/>
            </LinearGradientBrush>
        </Border.Background>
        -->

        <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="F1M0,0L2.667,2.66665 5.3334,0 5.3334,-1.78168 2.6667,0.88501 0,-1.78168 0,0z" Fill="#FF606060" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center"/>
        </Border>
    </Border>
    <ControlTemplate.Triggers>

        <!--Left out for brevity-->

    </ControlTemplate.Triggers>
</ControlTemplate>
like image 101
Funk Avatar answered Oct 09 '22 09:10

Funk