本文介绍了如何在WPF CheckBox控件中更改CheckMark的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个自定义模板复选框,我正在使用一个视图框(使用一个视图框来进行简单缩放)来实现,并且我不知道如何更改选中/未选中该复选框时显示的内容.
I have a custom template checkbox that I am implementing using a viewbox (using a viewbox to allow for simple scaling), and I can't figure out how to change out what is displayed when the checkbox is checked/unchecked.
我希望复选标记在被选中时为红色(不是最终外观,只是希望它能正常工作).
I would like for the checkmark to be red when checked (not the final look, just want to see it working).
我的复选框样式:
<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>
推荐答案
使用原始模板(此处).这是定义颜色的部分:
Go for the original template (here). This is the part where the color is defined:
<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>
对不起,再次抱歉,再次抱歉...我想这就是您想要的:
Sorry, sorry and sorry again... I think this is what you are looking for:
<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>
这篇关于如何在WPF CheckBox控件中更改CheckMark的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!