如何更改UWP复选框的背景?

时间:2018-06-26 03:18:18

标签: c# uwp styles controltemplate

我无法通过使用以下代码来更改复选框的背景,但它适用于Windows Store应用程序(Windows 8.1)。我想知道如何使其适用于UWP?

this.checkBox.Background = new SolidColorBrush(Windows.UI.Colors.Yellow);
this.checkBox.Foreground = new SolidColorBrush(Windows.UI.Colors.Blue);

1 个答案:

答案 0 :(得分:2)

要更改复选框的ForegroundBackground的颜色,您需要更新其模板。您可以执行以下操作-右键单击视觉设计器中的复选框,然后单击编辑模板> 编辑副本。这将为CheckBox创建默认模板。 (您可以检出整个模板here

此模板具有复选框的所有可视状态。您将需要覆盖所需的视觉状态。例如,您对"UncheckedNormal"状态执行类似的操作。

<VisualState x:Name="UncheckedNormal">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle"
                                           Storyboard.TargetProperty="Fill">
                  <DiscreteObjectKeyFrame KeyTime="0" Value="Your Fill Color here for only check part" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalRectangle"
                                           Storyboard.TargetProperty="Stroke">
                  <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                           Storyboard.TargetProperty="Foreground">
                  <DiscreteObjectKeyFrame KeyTime="0" Value="Your foreground color here" />
                </ObjectAnimationUsingKeyFrames>
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                           Storyboard.TargetProperty="Background">
                  <DiscreteObjectKeyFrame KeyTime="0" Value="Your background color here" />
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
</VisualState>

这也可以用C#编写,但是它太复杂了,建议使用XAML编辑样式。

希望这会有所帮助。请随时添加您可能有的任何问题。