2011-04-29 74 views
1

在我們的應用程序中,我們使用了一些假裝拖動&的裝飾。裝飾者不遵循鼠標,而是在鼠標移動時將其設置爲屏幕上的某些特定座標。Animate WPF Drag Adorner movement

double xPosLocation = (int)(dividerDistanceXAxis * virtualPosition) + YAxisData.SpacingLeft; 
double yPosLocation = CalculateValueToYPosition(VirtualPriceOfVehicle); 
DragAdorner.UpdatePosition(xPosLocation - offsetX, yPosLocation + offsetY); 

到目前爲止,這工作正常。但是用這樣的代碼,裝飾者從一個地方跳到另一個地方。這就是它應該如何工作,但如果裝飾者設置到一個新的位置,它會很順利地進行一些平滑的移動轉換。

可以這樣做嗎?由於我沒有看到位置屬性,我猜這是不可能做雙動畫或類似的東西。

回答

0

你必須使用一個DependencyProperty動畫

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

    // Using a DependencyProperty as the backing store for PositionProperty. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty PositionProperty = 
     DependencyProperty.Register("Position", typeof(Point), typeof(DragAdorner), new UIPropertyMetadata(PositionChanged)); 

    private static void PositionChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) 
    { 
     UpdatePosition(Position); 
    } 
} 

然後,你可以做這樣的事情(未測試)

double xPosLocation = (int)(dividerDistanceXAxis * virtualPosition) + YAxisData.SpacingLeft; 
double yPosLocation = CalculateValueToYPosition(VirtualPriceOfVehicle); 
Point newLocation = new Point(xPosLocation, yPosLocation); 

PointAnimation myPointAnimation = new PointAnimation(); 
myPointAnimation.From = DragAdorner.Position; 
myPointAnimation.To = newLocation; 
myPointAnimation.Duration = new Duration(TimeSpan.FromSeconds(1)); 

Storyboard myStoryboard = new Storyboard(); 
myStoryboard.Children.Add(myPointAnimation); 
Storyboard.SetTargetName(myPointAnimation, DragAdorner.Position); 
myStoryboard.Begin();