UWP - 更改按钮的背景颜色

UWP - change background color of button

我想在所有情况下更改按钮的默认背景。但是当我使用

button.Background = new SolidColorBrush(Windows.UI.Colors.Red);

如果按下按钮,我会得到一个红色按钮和灰色按钮。 我想我必须改变按钮的样式。但是如何设置样式颜色?

<VisualState x:Name="Pressed">
<Storyboard>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
    </ObjectAnimationUsingKeyFrames>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="BorderBrush">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
    </ObjectAnimationUsingKeyFrames>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
    </ObjectAnimationUsingKeyFrames>
    <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
</Storyboard>

你有这个动画:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
    <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}" />
</ObjectAnimationUsingKeyFrames>

您可以看到它在按下时将 RootGrid 的背景 属性 设置为 {ThemeResource SystemControlBackgroundBaseMediumLowBrush} 值。只需将其替换为您的颜色即可:

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid" Storyboard.TargetProperty="Background">
    <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
</ObjectAnimationUsingKeyFrames>