简体   繁体   English

PointAnimation定位自定义UserControl属性

[英]PointAnimation targeting custom UserControl property

I am attempting to animate the position of a UserControl I have created. 我试图设置我创建的UserControl的位置的动画。 My problem is very similar to this question asked on MSDN, without an answer sadly. 我的问题与MSDN上提出的问题非常相似,可悲的是没有答案。

https://social.msdn.microsoft.com/Forums/vstudio/en-US/11ce8aaa-1059-4fe5-8f4d-0fa7978e6ff2/using-pointanimation-to-move-a-control?forum=wpf https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/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. 总而言之,我创建了一个名为'Position'的Point类型依赖项属性,该属性用于指定UserControl在UI上的位置,并希望将UserConrol设置为新位置。

My problem is a syntax one i believe, i am unsure of the right syntax to target my UserControl property (in code-behind). 我的问题是我相信的一种语法,我不确定以我的UserControl属性为目标的正确语法(在代码后面)。

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. 对象'System.Windows.Point'不能用作PropertyPath的访问器参数。 An accessor parameter must be DependencyProperty, PropertyInfo, or PropertyDescriptor. 访问器参数必须是DependencyProperty,PropertyInfo或PropertyDescriptor。

Any alternatives to animate my control would also be greatly appreciated! 动画我的控制的任何替代方法也将不胜感激!

OK so problem solved. 好的,问题就解决了。 Use Expression Blend for all your animation needs. 使用Expression Blend满足您所有的动画需求。 The end ;) 结束 ;)

WPF allows to directly start an animation of a UIElement property without a Storyboard. WPF允许在没有情节提要的情况下直接启动UIElement属性的动画。

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. 请注意,上面没有设置PointAnimation的From属性。 That way the animation will start from the current value of the Position property. 这样,动画将从Position属性的当前值开始。


As an alternative to the Position property you may put your SubContainer control in a Canvas 作为Position属性的替代方法,您可以将SubContainer控件放在Canvas中

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

and animate its Canvas.Left and Canvas.Top properties: 并为其Canvas.LeftCanvas.Top属性设置动画:

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 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM