WPF Tweening Rectangle not working

DanielBA

New member
Joined
Jan 11, 2022
Messages
2
Programming Experience
3-5
I have made a custom wpf Control and I have been trying to create a method of Position Tweening to it. I have followed the Microsoft Docs explanations but to no avail. Can someone tell me why does this code don`t work?

The method:
NameScope.SetNameScope(_Rectangle, new NameScope());

RectAnimation a = new RectAnimation();
RectangleGeometry b = new RectangleGeometry(new Rect(this.Position.X, this.Position.Y, Size.X, Size.Y));

_Rectangle.RegisterName("AnimatedRectangleGeometry", b);

a.Duration = TimeSpan.FromMilliseconds(milliseconds);
a.FillBehavior = FillBehavior.HoldEnd;
a.RepeatBehavior = RepeatBehavior.Forever;
a.From = new Rect(this.Position.X, this.Position.Y, Size.X, Size.Y);
a.To = new Rect(endPosition.X, endPosition.Y, Size.X, Size.Y);
Storyboard.SetTargetName(a, "AnimatedRectangleGeometry");
Storyboard.SetTargetProperty(a, new PropertyPath(RectangleGeometry.RectProperty));
Storyboard RectStoryboard = new Storyboard();
RectStoryboard.Children.Add(a);

Path c = new Path();
c.Data = b;
c.Fill = new SolidColorBrush(Color.FromArgb((byte)(255 - BackgroundTransparency * 255), BackgroundColor.R, BackgroundColor.G, BackgroundColor.B));
c.StrokeThickness = StrokeThickness;
c.Stroke = new SolidColorBrush(Color.FromArgb((byte)(255 - StrokeTransparency * 255), StrokeColor.R, StrokeColor.G, StrokeColor.B));

c.Loaded += (Sender, e) => RectStoryboard.Begin(_Rectangle);

Canvas containerCanvas = new Canvas();
containerCanvas.Children.Add(c);

mainWindow.Content = containerCanvas;



Calling the method and presetting some variables:
ControlBase a = new ControlBase();
a.Position = new Vector2(100, 200);
a.Size = new Vector2(100, 100);
a.BackgroundColor = Color3.ReddishBrown;
a.TweenPosition(new Vector2(300, 400), 5000);


ControlBase is the class I made, Color3 can be reffered to as just Color with alpha = 255. Vector2 is just a combination of x and y.

If you need me to send any other property of said class, just say so.
 
Last edited:
Can you link to the documents you used for your tweening?
 
Back
Top Bottom