Roxy'Pro
Roxy'Pro

Reputation: 4454

WPF TextBox in IsEnabled="False" state is not applying correct background color

I'm having issue with applying colour to my textbox when it is disabled,

acctualy I have datagrid and I coloured row's with "#E0E4E5" colour. And I would like to keep colour of my textbox when it is disabled like colour of row is ("#E0E4E5").

What I did is next:

I set property to row AlternatingRowBackground="#E0E4E5" and Than I get this colour as row's background color.

After that I did next, I made style for my textbox because default one in wpf doesnt look nice, it has some shadows etc, so here is my custom style for textbox:

  <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="#E0E4E5" />
                <Setter Property="BorderBrush" Value="#E0E4E5" />
                <Setter Property="BorderThickness" Value="1.5" />
            </Trigger>
        </Style.Triggers>

        <Setter Property="BorderBrush" Value="#0091EA"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ScrollViewer x:Name="PART_ContentHost"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

As you can see guys there is this piece of code (triger) which is saying ok, when you are disabled let your background colour be like this and borderbrush be like this:

<Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="#E0E4E5" />
                <Setter Property="BorderBrush" Value="#E0E4E5" />
                <Setter Property="BorderThickness" Value="1.5" />
            </Trigger>
        </Style.Triggers>

So here is example of that how does it look :

enter image description here

As it is possible to see I applied same colours to both of them "#E0E4E5" but obliviously they are different, so guys how could I made it so when my textbox is disables its becoming "#E0E4E5" <- colour..

And I must notice also, if I change colour for border brush it is working. For example I set to both of them textbox and to borderbrush to be red when they are disabled, and I get this:

enter image description here

So border brush changed, but background did not.

Thanks guys, Cheers

Upvotes: 0

Views: 1853

Answers (1)

mm8
mm8

Reputation: 169400

Remove the Setter in the ControlTemplate that sets the Background to SystemColors.ControlBrushKey when the TextBox is disabled:

<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="#E0E4E5" />
            <Setter Property="BorderBrush" Value="#E0E4E5" />
            <Setter Property="BorderThickness" Value="1.5" />
        </Trigger>
    </Style.Triggers>

    <Setter Property="BorderBrush" Value="#0091EA"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                    Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ScrollViewer x:Name="PART_ContentHost"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 2

Related Questions