请看这个Checkbox
样式:
<Style TargetType="{x:Type CheckBox}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Height" Value="20" />
<Setter Property="FocusVisualStyle" Value="{DynamicResource MyFocusVisualStyte}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<BulletDecorator>
<BulletDecorator.Bullet>
<Grid Height="{TemplateBinding Height}"
Width="{Binding RelativeSource={RelativeSource Self}, Path=Height, UpdateSourceTrigger=PropertyChanged}"
MinHeight="20"
MinWidth="20"
ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="3*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="4*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="4*" />
</Grid.RowDefinitions>
<Border
Name="MainBorder"
Grid.ColumnSpan="9"
Grid.RowSpan="9"
CornerRadius="4"
BorderThickness="1"
Background="Transparent" />
<Border
Name="InnerBorder"
Grid.Column="1"
Grid.ColumnSpan="5"
Grid.Row="2"
Grid.RowSpan="5"
BorderThickness="1"
BorderBrush="Gray" />
<Path
Name="InnerPath"
Grid.Column="1"
Grid.ColumnSpan="5"
Grid.Row="2"
Grid.RowSpan="5"
Data="M31,5 L19.5,5 19.5,19.5 34.5,19.5 34.5,11.75"
Stretch="Fill"
Stroke="Gray"/>
<Path
Name="CheckMark"
Grid.Column="2"
Grid.ColumnSpan="5"
Grid.Row="1"
Grid.RowSpan="5"
Opacity="0"
Data="M9.07743946676476E-09,4.31805768640244L4.68740335877841,8.86361158398516C4.68740335877841,8.86361158398516,16.3281249985376,-2.42451336648723,16.3281249985376,-2.42451336648723L14.0622100581796,-4.77304938341948 4.68740335877846,4.31805791992662 2.22656251699567,1.93164208562579z"
Fill="Orange"
Stretch="Fill"
Stroke="Orange" />
<Path
Name="InderminateMark"
Grid.Column="3"
Grid.Row="4"
Data="M0,4 L1,5 5,1 4,0"
Opacity="0"
Stretch="Fill"
StrokeThickness="0"
Fill="#808080" />
</Grid>
</BulletDecorator.Bullet>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="CheckMark" Duration="0:0:0.2" To="1" />
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked" >
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="CheckMark" Duration="0:0:0.2" To="0" />
</Storyboard>
</VisualState>
<VisualState x:Name="Indeterminate">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
Storyboard.TargetName="InderminateMark" Duration="0:0:0.2" To="1" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<ContentPresenter
Margin="4,0,4,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True" />
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="InnerBorder" Property="BorderBrush" Value="#FF7AA0CD" />
<Setter TargetName="InnerPath" Property="Stroke" Value="#FF7AA0CD" />
<Setter TargetName="CheckMark" Property="Fill" Value="Gainsboro"/>
<Setter TargetName="CheckMark" Property="Stroke" Value="Gainsboro"/>
</Trigger>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
<Setter Property="Foreground" Value="#303030"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="MainBorder" Property="Background" Value="#81d2eb" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="Gainsboro"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="#55B3B3B6"/>
<Setter TargetName="CheckMark" Property="Fill" Value="Orange" />
<Setter TargetName="CheckMark" Property="Stroke" Value="Orange" />
<Setter TargetName="InnerPath" Property="Stroke" Value="#55649EB4" />
<Setter TargetName="InderminateMark" Property="Fill" Value="#cccccc" />
<Setter TargetName="InnerBorder" Property="BorderBrush" Value="#55649EB4" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
因此,当我检查Checkbox
时,我尝试为Foreground
Color
添加非常简单的style
:
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
<Setter Property="Foreground" Value="#303030"/>
</Trigger>
由于某种原因,这不起作用,Foreground
颜色不会改变。 任何建议为什么?
更新
这是我的Checkbox
:
<CheckBox
FontSize="14"
Height="25"
FontFamily="Ariel"/>
如您所见,我没有设置在这里Foreground
财产。
以下是一些触发器的顺序:
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
<Setter Property="Foreground" Value="Red"/>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="MainBorder" Property="Background" Value="#81d2eb" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="Gainsboro"/>
</Trigger>
(为了显而易见,我将你的"如果检查"前景更改为红色。
如果选中,请将前景设置为红色(或在本例中为 #303030(。
然后,如果未选中,请折叠 InnerBorder。
然后,如果按下,请设置主边框的背景。
最后,如果已启用,请将"前景"设置为"庚斯伯勒"。
因此,每当启用它时,它都会将前景设置为Gainsboro。
您正在覆盖 IsChecked 前台。简单的修复,你已经想通了:
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="MainBorder" Property="Background" Value="#81d2eb" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Foreground" Value="Gainsboro"/>
</Trigger>
<!--
Move this one so it comes after IsEnabled="True", but before IsEnabled="False"
-->
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />
<Setter Property="Foreground" Value="Red"/>
</Trigger>
你不需要明确地将Foreground
应用于ContentPresenter
,那是衰老开始的。