我有一个用于开关控件的颜色动画,并且让背景颜色可以变换我想将父控件的Background
属性绑定到To
ColorAnimation
属性。我尝试了很多,但没有人工作。我怎么能这样做?
切换控件样式:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<Viewbox Stretch="Uniform">
<Border x:Name="layer" Width="35" Height="20" CornerRadius="10,10,10,10"
Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Canvas Canvas.Top="0" Canvas.Left="0">
<Ellipse x:Name="circle" Fill="Gray" Stroke="DarkGray" StrokeThickness="0"
Width="20" Height="20">
<Ellipse.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</Ellipse.RenderTransform>
</Ellipse>
</Canvas>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
To="LightGreen"
Duration="0:0:0.3"/>
<DoubleAnimation Storyboard.TargetName="circle"
Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
To="15"
Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
Duration="0:0:0.3"
To="{TemplateBinding Background}"/>
<DoubleAnimation Storyboard.TargetName="circle"
Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
To="0"
Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Style>
ColorAnimation部分:
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
To="{TemplateBinding Background}"
Duration="0:0:0.3"/>
答案 0 :(得分:0)
您无法在彩色动画中使用绑定,否则您将收到此错误 &#34;无法冻结此Storyboard时间轴树以供跨线程使用&#34; ,有关此内容的更多信息: WPF Animation "Cannot freeze this Storyboard timeline tree for use across threads"
现在,如果你想要一个解决方法,你可以做的是创建一个静态资源,代表你父控件的背景颜色,如下所示:
$L = ipcsv ln.csv |Select-Object -ExpandProperty Letter |Where-Object {$_} |Get-Random
$N = ipcsv ln.csv |Select-Object -ExpandProperty Number |Where-Object {$_} |Get-Random
然后您可以将此资源分配给您的颜色动画属性,如下所示:
<Color A="255"
R="100"
G="0"
B="0"
x:Key="resource" />
希望它有所帮助。