2013-05-29 13 views
0

我在1x1網格上有一個WPF Image,我試圖移動到新的位置。我使用下面的代碼:如何將WPF TranslateTransform與Margin一起用於將圖像移動到正確的位置

public void AnimateImage(int seatNum, int cardNum) 
{ 
    // This method animates dealing a cardback image to the specified seat. 
    // 
    Point[][,] Margins; 
    Margins = CardMargins; // CardMargins contains the positions to where the cards should end up after being moved. 
    Image CardbackImage1 = CardbackImages[seatNum, cardNum]; 
    // Specify the starting position of the image. 
    CardbackImage1.Margin = new Thickness(DealerStartX, DealerStartY, 0, 0); 
    CardsGrid.Children.Insert(seatNum, CardbackImage1); 
    // Note that if instead of specifying the starting point, the to-point is specified instead, 
    // and a return inserted at this point - it displays the card in the correct location. 
    TranslateTransform trans = new TranslateTransform(); 
    CardbackImage1.RenderTransform = trans; 
    double ToPosX = Margins[NumSeats][seatNum, cardNum].X; 
    double ToPosY = Margins[NumSeats][seatNum, cardNum].Y; 
    // DoubleAnimation CardsAnimX = new DoubleAnimation(DealerStartX, 65, TimeSpan.FromSeconds(DealAnimateTime)); 
    DoubleAnimation CardsAnimX = new DoubleAnimation(DealerStartX, ToPosX, TimeSpan.FromSeconds(DealAnimateTime)); 
    // DoubleAnimation CardsAnimY = new DoubleAnimation(DealerStartY, DealerStartY - 50, TimeSpan.FromSeconds(DealAnimateTime)); 
    DoubleAnimation CardsAnimY = new DoubleAnimation(DealerStartY, ToPosY, TimeSpan.FromSeconds(DealAnimateTime)); 
    trans.BeginAnimation(TranslateTransform.XProperty, CardsAnimX); 
    trans.BeginAnimation(TranslateTransform.YProperty, CardsAnimY); 
} 

CardMargins陣列用於保存的凡卡應該結束了位置。卡動畫,但問題是卡沒有結束在適當的位置。我已經驗證CardMargins數組內的值是否正確以及數組的索引。我懷疑TranslateTransform方法要麼使用不同類型的座標,要麼可能使用不同的座標系。

可以將邊距用作動畫屬性嗎?如果是這樣,是否需要使用任何類型的轉換以便與TranslateTransform一起使用?有沒有更好的方式來動畫圖像而不使用保證金?

回答

1

這可能是簡單的使用ThicknessAnimation

實例進行動畫處理的Margin屬性本身:

ThicknessAnimation CardAni = new ThicknessAnimation(StartMargin, EndMargin, TimeSpan.FromSeconds(DealAnimateTime)); 
CardbackImage1.BeginAnimation(Image.MarginProperty, CardAni); 
+0

謝謝。問題解決了。您的代碼建議會使卡片在最終的位置出現。 –

相關問題