UWP - 更改按钮的背景颜色

时间:2016-03-18 01:26:17

标签: c# button background styles win-universal-app

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

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>

1 个答案:

答案 0 :(得分:0)

你有这个动画:

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

您可以看到它在按下时将RootGrid的Background属性设置为{ThemeResource SystemControlBackgroundBaseMediumLowBrush}值。只需用你的颜色替换它就可以了:

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