Reputation: 16119
I'm having difficulties animating a render transform of a Canvas object using a data trigger. As a test case I have an ellipse on a Canvas along with two buttons. When I press each button I want the position of the ellipse to animate to that button's X coordinate:
The fact that I'm using buttons in this particular example is neither here-nor-there, the point is I'm trying to trigger these animations with a DataTrigger bound to a property in my view model:
// this is the property my DataTrigger will bind to
private string _Anim;
public string Anim
{
get { return this._Anim; }
set
{
this._Anim = value;
RaisePropertyChanged(() => this.Anim);
}
}
// a command handler for the buttons that sets my animation property
public ICommand AnimCommand { get { return new RelayCommand<string>(OnAnim); } }
private void OnAnim(string value)
{
this.Anim = String.Empty; // cancel any existing animation, probably not needed...
this.Anim = value;
}
Here's the Xaml I'm using to do this. It's basically just a canvas containing an ellipse and two buttons, and the ellipse has 2 storyboards that get triggered in response to the backend property being set to different strings (i.e. "Left" and "Right"):
<Viewbox xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Canvas Width="350" Height="150" Background="CornflowerBlue">
<Ellipse x:Name="MyEllipse" Width="20" Height="20" Fill="Red">
<Ellipse.RenderTransform>
<TranslateTransform x:Name="myTransform" X="50" Y="50" />
</Ellipse.RenderTransform>
<Ellipse.Style>
<Style TargetType="Ellipse">
<Style.Triggers>
<DataTrigger Binding="{Binding Anim}" Value="Left">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)" To="100" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding Anim}" Value="Right">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)" To="200" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>
</Ellipse.Style>
</Ellipse>
<Button x:Name="btnLeft" Content="Left" Command="{Binding AnimCommand}" CommandParameter="Left" Canvas.Left="100" Canvas.Top="100" Width="32" Height="24" />
<Button x:Name="btnRight" Content="Right" Command="{Binding AnimCommand}" CommandParameter="Right" Canvas.Left="200" Canvas.Top="100" Width="32" Height="24" />
</Canvas>
</Viewbox>
When I run this I can click on the first animation and then the second, and the behaviour is correct both times. However clicking on them in reverse order doesn't work...the "right" button animates correctly and the "left" button does nothing. It's as if the "right" animation is still running or locking down the property or something, a case further backed up by the fact that the situation is reversed if I swap the order of the storyboard declarations in the Xaml.
This works fine with EventTriggers, I'm only seeing it with DataTriggers. I've also tried playing with the FillBehaviours and HandoffBehaviors, to no avail. Can someone please explain why this is happening and how to fix it?
Upvotes: 3
Views: 3881
Reputation: 169150
You should remove the Storyboard
once the animation has completed. You could use the RemoveStoryboard class to do this. Just give the BeginStoryboard
element a Name
and then add a RemoveStoryboard
element to the ExitActions
of the DataTrigger
:
<Style TargetType="Ellipse">
<Style.Triggers>
<DataTrigger Binding="{Binding Anim}" Value="Left">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sb">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)" To="100" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sb" />
</DataTrigger.ExitActions>
</DataTrigger>
<DataTrigger Binding="{Binding Anim}" Value="Right">
<DataTrigger.EnterActions>
<BeginStoryboard Name="sb2">
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)" To="200" Duration="0:0:1" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sb2" />
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
The other option would be set the FillBehavior
of the Storyboard
to Stop
but then the position of the Ellipse
will be restored once the animation has completed.
Doesn't work I'm afraid. Clicking Left then Right works at first, but then clicking Left again causes the animation to restart from position 0, even if I set the HandoffBehaviors and FillBehaviours.
Get rid of the DataTriggers
in your XAML and write some custom code then. This works:
private void MyEllipse_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
INotifyPropertyChanged inpc = MyEllipse.DataContext as INotifyPropertyChanged;
if (inpc != null)
WeakEventManager<INotifyPropertyChanged, PropertyChangedEventArgs>.AddHandler(inpc, nameof(INotifyPropertyChanged.PropertyChanged), OnChanged);
}
private PropertyInfo _pi;
private void OnChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Anim")
{
_pi = _pi ?? sender.GetType().GetProperty(e.PropertyName);
string anim = _pi?.GetValue(sender) as string;
switch (anim)
{
case "Left":
myTransform.BeginAnimation(TranslateTransform.XProperty, new DoubleAnimation() { To = 100.0, Duration = TimeSpan.FromSeconds(1) });
break;
case "Right":
myTransform.BeginAnimation(TranslateTransform.XProperty, new DoubleAnimation() { To = 200.0, Duration = TimeSpan.FromSeconds(1) });
break;
}
}
}
XAML:
<Viewbox xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Canvas Width="350" Height="150" Background="CornflowerBlue">
<Ellipse x:Name="MyEllipse" Width="20" Height="20" Fill="Red"
DataContextChanged="MyEllipse_DataContextChanged">
<Ellipse.RenderTransform>
<TranslateTransform x:Name="myTransform" X="50" Y="50" />
</Ellipse.RenderTransform>
</Ellipse>
<Button x:Name="btnLeft" Content="Left" Command="{Binding AnimCommand}" CommandParameter="Left" Canvas.Left="100" Canvas.Top="100" Width="32" Height="24" />
<Button x:Name="btnRight" Content="Right" Command="{Binding AnimCommand}" CommandParameter="Right" Canvas.Left="200" Canvas.Top="100" Width="32" Height="24" />
</Canvas>
</Viewbox>
Upvotes: 4