0
   <Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Margin" Value="5,5,5,5"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Background" Value="Red"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1">
                            <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                                <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

This is a part of my App.xaml code.

It is style for textBox.

But Background Property didn't work.

Others work well.

Please help me. Why can't I change background color of TextBox?

1
  • Control template is causing the problem, remove ControlTemplate and try it should work Commented May 2, 2017 at 5:36

1 Answer 1

1

You are giving a background to your text box, instead you should give the background to your border because it has been defined in the control template.

    <Style x:Key="TextInputStyle" TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="Margin" Value="5,5,5,5"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="AliceBlue"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Border x:Name="bg" BorderBrush="#FF7F98DC" BorderThickness="1" ***Background="Red"***>
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="bg" Value="#FF7E97F0"/>
                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                        </Trigger>
                        <Trigger Property="IsFocused" Value="True">
                            <Setter Property="BorderBrush" TargetName="bg" Value="DarkBlue"/>
                            <Setter Property="BorderThickness" TargetName="bg" Value="2"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Please see the Border tag inside ControlTemplate above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.