I have a color animation for a switch control and to let the background color to be changable I want to bind the Background
property of the parent control to the To
property of ColorAnimation
. I tried a lot but no one worked. How can I do that?
Switch control style:
<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 part:
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
To="{TemplateBinding Background}"
Duration="0:0:0.3"/>