如何在WPF中设置CheckBox的背景颜色

6
在WinForm中,我们可以设置CheckBox的BackColor。
在WPF中如何做到呢?我尝试过:
<CheckBox Content="CheckBox" Background="Red"/>

但这只会改变矩形边框的颜色

输入图片描述

我还尝试了

<CheckBox>
    <TextBlock Text="CheckBox" Background="Red"/>
</CheckBox>

但是这只改变了文本的背景颜色,而不包括矩形。 enter image description here =======
谢谢大家提供的解决方案。我觉得最简单的方法适合我 :)

3个回答

10

如果你稍微尝试一下面板,你就会到达那里:

<Grid Background="Red" HorizontalAlignment="Left">
  <CheckBox Content="test" />
</Grid>

非常接近你想要的。

我亲自试过了 ;-)


4
<Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <CheckBox FlowDirection="RightToLeft" background="Red"
                          IsChecked="False" />
                <Border Grid.Column="1"
                        Margin="20 0 0 0"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Center"
                        Background="LightGray">
                    <TextBlock HorizontalAlignment="Left"
                               Foreground="Black"
                               Style="{StaticResource RegularTextblock}"
                               Text="Checkbox1" />
                </Border>
            </Grid>

1

你要求的有点过多。我使用Blend创建了一个适用于CheckBox的相关样式。

代码太长,所以SO不允许显示。这里是pastie的链接link

对于Background,有一个网格markGrid。我添加了一个Background和一个TemplateBinding来强制CheckBox改变颜色。缺点是如果背景很暗,则路径颜色将非常微弱可见。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接