wpf - WPF Datatrigger ColorAnimation 不起作用
问题描述
我目前正在为所有 wpf 控件开发自己的主题。我真的无法让 Checkbox Usercontrol 正常工作。选中复选框时,我试图获得平滑的颜色淡入/淡出。这是我的资源字典中我的代码的相关部分:
<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>
淡出有效(Value=false),但淡入永远不会触发。我添加了一个“From”值来确认/测试这一点,它根本不会触发。所有帮助将不胜感激!
亲切的问候
解决方案
正确的方法是使用EnterAction
andExitAction
的Trigger
and 将触发器从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>
推荐阅读
- c# - 有没有办法可以循环显示引导弹出内容?
- java - Sqlite 中是否有特定的有效表名
- php - 使用 $stmt->get_result()->fetch_assoc() 时出现 PHP 错误(500 内部服务器错误);
- django - Github webhook 向我的 django 应用返回 404
- python - 关于 lambda 的另一个问题,“k”在这里是什么意思?
- pandas - 熊猫索引的时间范围
- javascript - 使用循环和语句进行数组排序
- tensorflow - 您如何获得 keras 中单个类类型的准确性?
- android - 当任何视图的大小发生变化或 EditText 获得焦点时,BottomSheetDialogFragment 会跳转到状态栏的顶部
- javascript - openlayers 动态图像大小