2011-08-01 102 views
0

我是WPF的新手。我實現了可以通過鼠標移動的網格的全屏應用(drag'n'drop style)。如果網格控件超出屏幕邊界,它將以簡單的動畫返回到其默認狀態。問題是,當網格返回它可以被移動更多!提供的一些代碼片段:操縱接口和動畫

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.TransformToAncestor(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); 
    } 
} 

主要類:

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

我沒有任何想法。任何幫助,將不勝感激!

回答

0

確保將FillBehavior屬性添加到所有的動畫...

xAnimation.FillBehavior = FillBehavior.Stop;

這可能會導致你帶回到它在哪裏動畫之前相同位置的電網......

您可以在動畫的結尾處理此手動設置屬性..

一些事情這樣

在這裏,你是給價值動畫...

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

在設置直接相同值的動畫結束...

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