如何更改通用Windows平台中CheckBox控件的默认样式?

Har*_*ari 3 xaml win-universal-app windows-10 uwp

我不知道如何更改复选框的默认颜色。我为复选框编写了这一行

<CheckBox  x:Name="chkRememberme" Foreground="Gray"  Grid.Row="4" Background="Transparent"   IsChecked="False" TabIndex="3"  HorizontalAlignment="Center" VerticalAlignment="Top" Background="Blue"   Margin="0,2,0,0"  />
Run Code Online (Sandbox Code Playgroud)

在下图中,我提到了我需要的复选框的样式。

在此输入图像描述

Rom*_*asz 5

打开设计器窗口,右键单击您的CheckBox,然后选择Edit template-> Edit a copy(或在 Blend 中执行相同操作)。这应该在您的资源中创建默认样式(您也可以在 MSDN 中找到该样式)。

在风格中你会找到你想要的一切。您正在寻找的背景颜色由 VisualStateManager 更改 - 编辑合适的视觉状态,它应该可以工作。示例 - 将NormalRectangle.Fill属性的值更改为SystemControlBackgroundBaseLowBrush

<VisualState x:Name="CheckedNormal">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="NormalRectangle">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <DoubleAnimation Duration="0" To="{ThemeResource CheckBoxCheckedStrokeThickness}" Storyboard.TargetProperty="StrokeThickness" Storyboard.TargetName="NormalRectangle"/>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="NormalRectangle">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltTransparentBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="CheckGlyph"/>
    </Storyboard>
</VisualState>
Run Code Online (Sandbox Code Playgroud)

示例图片:

在此输入图像描述

另请注意,您可能还需要编辑上述视觉状态之外的其他视觉状态 - 这取决于您的需要。