开发者

ManipulationDelta and Animation

I'm a newbie in WPF. I implemented fullscreen app with grids that could be moved over by mouse (drag'n'drop style). If the grid control arrears out of the screen bounds it returns to its default state with simple animation. The problem is when grid returns it could be moved any more! Some code snippets provided:

public partial class MenuCard : UserControl, ITouchObject, INotifyPropertyChanged
{
    ...
    public static readonly DependencyProperty XProperty =
        DependencyProperty.Register("X", typeof(double), typeof(MenuCard), new UIPropertyMetadata(0.0, OnPosXChanged, CourceXValue));
    ...
    private static void OnPosXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var owner = (MenuCard) d;
        Grid rootGrid = (Grid)Application.Current.MainWindow.FindName("rootGrid");
        Point actual = owner.TransformToAncestor(rootGrid).Transform(new Point(owner.X, owner.Y));
        Point topLeft = owner.TransformToAncesto开发者_JS百科r(rootGrid).Transform(new Point(0, 0));
        Point bottomRight = owner.TransformToAncestor(rootGrid).Transform(new Point(owner.ActualWidth, owner.ActualHeight));

        if (actual.X + (bottomRight.X - topLeft.X) <= border || actual.X >= SystemParameters.PrimaryScreenWidth - border)
        {
            ReturnToDefault(owner);
        }
    }
    ...
    private static void ReturnToDefault(MenuCard owner)
    {
        DoubleAnimation yAnimation = new DoubleAnimation();
        yAnimation.From = owner.Y;
        yAnimation.DecelerationRatio = 0.5;
        yAnimation.To = (double)YProperty.DefaultMetadata.DefaultValue;
        owner.BeginAnimation(MenuCard.YProperty, yAnimation);

        DoubleAnimation xAnimation = new DoubleAnimation();
        xAnimation.From = owner.X;
        xAnimation.DecelerationRatio = 0.5;
        xAnimation.To = (double)XProperty.DefaultMetadata.DefaultValue;
        owner.BeginAnimation(MenuCard.XProperty, xAnimation);
    }
}

Main class:

public partial class MainWindow : Window
{

    private void CanvasManipulationDelta(object sender, ManipulationDeltaEventArgs e)
    {
        var p = GetSender(e.ManipulationContainer as FrameworkElement);     
        if (p != null)
        {
            ManipulationDelta md = e.DeltaManipulation;
            p.X += md.Translation.X;
            p.Y += md.Translation.Y;
            p.Angle += md.Rotation;
            p.ScaleX *= md.Scale.X;
            p.ScaleY *= md.Scale.Y;
        }
        e.Handled = true;
    }

    private ITouchObject GetSender(FrameworkElement element)
    {
        while (true)
        {
            if (element.Parent == null)
            {
                return null;
            }
            if (element is ITouchObject)
            {
                return element as ITouchObject;
            }
            element = element.Parent as FrameworkElement;
        }
    }
}

I have no ideas. Any help would be appreciated!


make sure to add the FillBehavior property to all animations...

xAnimation.FillBehavior = FillBehavior.Stop;

This may result you the grid taken back to the same position where it is before animation...

You can handle this at the end of animation be setting the property manually..

some thing like this

Here you are giving To value to animation...

yAnimation.To = (double)YProperty.DefaultMetadata.DefaultValue;

at the end of the animation you set the same value directly...

MenuCard.YProperty = (double)YProperty.DefaultMetadata.DefaultValue;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜