我目前正在为所有wpf控件制作自己的主题。我真的不能让复选框用户控件工作。我试图得到一个平滑的颜色渐入/渐出时,检查复选框。这是我的资源字典中代码的相关部分:
<Border x:Name="checkbox" CornerRadius="5" Width="18" Height="18" BorderThickness="1" BorderBrush="Black">
<!-- Checkmark -->
<TextBlock Text="" ClipToBounds="True" FontFamily="Segoe MDL2 Assets" HorizontalAlignment="Left"
Background="Transparent" Foreground="White" FontSize="15" FontWeight="ExtraBold"/>
<Border.Style>
<Style TargetType="Border">
<!-- Animations (Color fade in and out) -->
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#006092" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked}" Value="False">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#FFFFFF" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
淡出工作(值=false),但淡出永远不会触发。我加了一个"From"值来确认/测试,它根本不会触发。所有的帮助将非常感激!
亲切的问候
正确的方法是使用Trigger
的EnterAction
和ExitAction
,并将触发器从Style
移动到ControlTemplate
。还要注意Border
属性是如何连接到模板化的父属性以允许局部值产生影响的。
<CheckBox IsChecked="True" BorderThickness="1" BorderBrush="Black">
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<Border x:Name="checkbox"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
CornerRadius="5"
Width="18" Height="18" >
<!-- Checkmark -->
<TextBlock Text="" ClipToBounds="True" FontFamily="Segoe MDL2 Assets" HorizontalAlignment="Left"
Background="Transparent" Foreground="White" FontSize="15" FontWeight="ExtraBold"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#006092" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation To="#FFFFFF" Duration="0:0:0.2" Storyboard.TargetProperty="Background.Color"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>