【问题标题】:How to change the color of the CheckMark in WPF CheckBox control?如何更改 WPF CheckBox 控件中 CheckMark 的颜色?
【发布时间】:2012-03-15 19:52:13
【问题描述】:

我有一个自定义模板复选框,我正在使用视图框(使用视图框以允许简单缩放)实现该复选框,但我不知道如何更改选中/取消选中复选框时显示的内容。

我希望复选标记在选中时显示为红色(不是最终外观,只是想看看它是否正常工作)。

我的复选框样式:

<Style x:Key="KioskCheckBox" TargetType="{x:Type CheckBox}">
        <Setter Property="FontFamily" Value="{StaticResource FontFamilyLight}" />
        <Setter Property="FontSize" Value="{StaticResource KioskNormalFontSize}" />
        <Setter Property="Foreground" Value="{StaticResource brshSystemTextColor}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">

                        <!--<Viewbox HorizontalAlignment="Left" 
                                 VerticalAlignment="Top"
                                 Stretch="Fill" 
                                 Height="30"   
                                 Width="30" Margin="10,0,0,0">
                            <Grid Height="200" Width="200">
                                <Ellipse  
                                Fill="Transparent"
                                StrokeThickness="15" 
                                Stroke="{StaticResource brshSystemTextColor}"/>
                                <Path 
                                Stroke="{StaticResource brshSecondaryColor}"
                                Fill="Transparent" 
                                Stretch="None"
                                StrokeThickness="20"
                                Data="M 30,100 L 80,140 L 160,60" Margin="0,0,2,2"/>
                            </Grid>
                        </Viewbox>-->
                        <ContentControl>
                        <StackPanel Orientation="Horizontal">
                            <Viewbox HorizontalAlignment="Left" 
                                VerticalAlignment="Center"
                                Stretch="Fill" 
                                Height="30"   
                                Width="30" Margin="10,0,0,0">
                                <Grid Height="200" Width="200">
                                    <Ellipse  
                                        Fill="Transparent"
                                        StrokeThickness="15" 
                                        Stroke="{StaticResource brshSystemTextColor}"/>
                                    <Path 
                                        Stroke="{StaticResource brshSecondaryColor}"
                                        Fill="Transparent" 
                                        Stretch="None"
                                        StrokeThickness="20"
                                        Data="M 30,100 L 80,140 L 160,60" Margin="0,0,2,2"/>
                                </Grid>
                            </Viewbox>
                            <TextBlock Text="{TemplateBinding Content}" Margin="10,0,0,0" />
                        </StackPanel>
                    </ContentControl> 
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">

                            <!-- Running into problems here. -->

                        </Trigger>
                        <Trigger Property="IsChecked" Value="False">

                        </Trigger>
                    </ControlTemplate.Triggers>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

    标签: wpf controltemplate


    【解决方案1】:

    使用原始模板 (here)。这是定义颜色的部分:

    <Path Visibility="Collapsed"
          Width="7"
          Height="7"
          x:Name="CheckMark"
          SnapsToDevicePixels="False"
          StrokeThickness="2"
          Data="M 0 0 L 7 7 M 0 7 L 7 0">
      <Path.Stroke>
        <SolidColorBrush Color="{DynamicResource GlyphColor}" />
      </Path.Stroke>
    </Path>
    

    对不起,对不起,再次抱歉......我想这就是你要找的:

    <ControlTemplate TargetType="CheckBox">
      <Grid>
        <Path x:Name="Equis"
              Opacity="0"
              Stroke="Red"
              Fill="Red"
              Stretch="UniformToFill"
              StrokeThickness="20"
              Data="M 30,100 L 80,140 L 160,60"
              Margin="0,0,2,2" />
        <ContentPresenter Margin="4"
                          HorizontalAlignment="Left"
                          VerticalAlignment="Top" />
      </Grid>
      <ControlTemplate.Triggers>
        <Trigger Property="IsChecked"
                 Value="true">
          <Setter TargetName="Equis"
                  Property="Opacity"
                  Value="1" />
        </Trigger>
        <Trigger Property="IsChecked"
                 Value="false">
          <Setter TargetName="Equis"
                  Property="Opacity"
                  Value="0" />
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
    

    【讨论】:

    • 对不起,我的无知,但你能把它扔进 并发布它,以便我可以更好地了解您在告诉我的内容?
    • AGoodDisplayName:嗯,您说“我希望选中标记时显示为红色”,默认模板使用我发布的代码来绘制选中标记。 Blam:我猜当复选框的值为 true 时,“Collapsed”被修改为“Visible”。
    • @NestroArturo,抱歉回复晚了。直到今天我才看到你的编辑。也很有趣,我昨天自己想通了,完全按照你在你的例子中展示的那样做,所以谢谢你的回答。
    • 很酷的解决方案,但对我来说,在应用这种样式后并不是每次点击都会受到尊重 - 其他人有这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 2012-01-17
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多