Change backgorund of button in wpf when it has focus

button, c#, wpf

Solution

I believe that your problem is caused by the default `ControlTemplate` for the `Button` control. The colour animations that you describe are defined in this `ControlTemplate` and so you need to provide your own `ControlTemplate` to remove this behaviour:

<Button Content="Click Me" Background="Blue">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Border Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Margin="{TemplateBinding Padding}" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="Button.IsFocused" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="White" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

Problem

I have a button and I want to change the background color of button. but when I set the background property to a color like blue: ``` <Button Width="75" Height="25" Margin="6" Background="Blue"/> ``` and when the button has focus, the color changes in white and my color. How can I set this white color to another color?

Original source

Related problems