Liliha Celpa
Liliha Celpa

Reputation: 33

PointAnimation targeting custom UserControl property

I am attempting to animate the position of a UserControl I have created. My problem is very similar to this question asked on MSDN, without an answer sadly.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/11ce8aaa-1059-4fe5-8f4d-0fa7978e6ff2/using-pointanimation-to-move-a-control?forum=wpf

To summarize, I have a created a Point type dependency property named 'Position' which I use to specify the UserControl's place on the UI, and would like to animate the UserConrol to a new location.

My problem is a syntax one i believe, i am unsure of the right syntax to target my UserControl property (in code-behind).

public class SubContainer : Control 
{ 
 public static readonly DependencyProperty PositionProperty; 

 static SubContainer() 
 { 
    DefaultStyleKeyProperty.OverrideMetadata(typeof(SubContainer), 
        new FrameworkPropertyMetadata(typeof(SubContainer))); 

    PositionProperty = DependencyProperty.Register( 
        "Position", 
        typeof(Point), 
        typeof(SubContainer), 
        new PropertyMetadata(new Point(0, 0))); 
 } 

 public Point Position 
 { 
    get { return (Point)GetValue(PositionProperty); } 
    set { SetValue(PositionProperty, value); } 
 } 

And then my Animation:

        public void BoxTransition()
    {
        Point destination = new Point(70, 300);
        SubContainer box = (MainContent.Children[0] as Container).Children[0] as SubContainer;
        PointAnimation transition = new PointAnimation();
        TimeSpan timespan = TimeSpan.FromSeconds(2);

        this.RegisterName("Target", box.Position);
        Transition.From = box.Position;
        Transition.To = destination;
        Storyboard.SetTargetName(transition, "Target");
        Storyboard.SetTargetProperty(transition, new PropertyPath(box.Position));
        Storyboard bTransition = new Storyboard();
        bTransition.Children.Add(transition);
        bTransition.Begin();

    }

I get the following error:

Object 'System.Windows.Point' cannot be used as an accessor parameter for a PropertyPath. An accessor parameter must be DependencyProperty, PropertyInfo, or PropertyDescriptor.

Any alternatives to animate my control would also be greatly appreciated!

Upvotes: 0

Views: 350

Answers (2)

Clemens
Clemens

Reputation: 128013

WPF allows to directly start an animation of a UIElement property without a Storyboard.

You wouldn't need more code than this:

var transition = new PointAnimation
{
    To = new Point(70, 300),
    Duration = TimeSpan.FromSeconds(2)
};

box.BeginAnimation(SubContainer.PositionProperty, transition);

Note that the above does not set the From property of the PointAnimation. That way the animation will start from the current value of the Position property.


As an alternative to the Position property you may put your SubContainer control in a Canvas

<Canvas>
    <local:SubContainer x:Name="box" Canvas.Left="0" Canvas.Top="0" .../>
</Canvas>

and animate its Canvas.Left and Canvas.Top properties:

var duration = TimeSpan.FromSeconds(2);
box.BeginAnimation(Canvas.LeftProperty,
    new DoubleAnimation { To = 70, Duration = duration });
box.BeginAnimation(Canvas.TopProperty,
    new DoubleAnimation { To = 300, Duration = duration });

Upvotes: 1

Liliha Celpa
Liliha Celpa

Reputation: 33

OK so problem solved. Use Expression Blend for all your animation needs. The end ;)

Upvotes: 0

Related Questions