How to keep the background of a button the same wh

2019-07-25 17:28发布

问题:

Possible Duplicate:
WPF Button isPressed and isEnabled problem

Here is a snippet of code from an application that I am developing using WPF and XAML. What I am trying to do is get the button's background to stay the same color when it is clicked on by the user as it is when it is not clicked on by the user.

Note: There is an image in the button.

<Button Name="Button1" Background="#3852A4" Grid.Column="0" Grid.Row="0" Width="35" Height="35" HorizontalAlignment="Left" VerticalAlignment="Top" Click="swapGd1andGd2">
    <Button.Style>
        <Style>
            <Style.Triggers>
                <Trigger Property="Button.IsPressed" Value="True">
                    <Setter Property="Button.Background" Value="#3852A4" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <Image Source="Images/112_DownArrowShort_Grey_24x24_72.png" Width="24" Height="24" Stretch="Fill"></Image>
</Button>`

My question is, why doesn't this code produce the results that I want?

回答1:

In wpf buttons have a default template that overrides style values. See here for an example of how to change the value using a template.