据我了解,您想在用户按下按钮后更改背景一次?如果是这样,您可以将样式与动画一起使用:
<Window x:Class="ButtonBackground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontSize" Value="100"/>
<Setter Property="Foreground" Value="White"/>
<Style.Triggers>
<EventTrigger RoutedEvent="Click">
<BeginStoryboard>
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Background)" >
<ObjectAnimationUsingKeyFrames.KeyFrames>
<DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame.Value>
<SolidColorBrush Color="Black"/>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame.Value>
<!--<SolidColorBrush Color="Pink"/>-->
<ImageBrush ImageSource="{Binding MyImgSource}" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames.KeyFrames>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Content="1" Style="{StaticResource ButtonStyle}" />
</Grid>
</Window>
如果您想在按下按钮后返回黑色背景,那么您的样式将是:
<Style x:Key="ButtonStyle" TargetType="Button">
<Setter Property="Background" Value="Black"/>
<Setter Property="FontSize" Value="100"/>
<Setter Property="Foreground" Value="White"/>
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background">
<Setter.Value>
<!--<SolidColorBrush Color="Pink"/>-->
<ImageBrush ImageSource="{Binding MyImgSource}" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>