我動畫的邊界在Silverlight調整,但是我還需要逐步消除周圍的保證金(目前爲50)。混合似乎不會爲邊距變化生成補間 - 它只是一次從50跳到0。有沒有辦法做到這一點?動畫保證金變化在Silverlight
7
A
回答
8
問題是,邊距實際上是不是依賴項對象的類型「System.Windows.Thickness」,因此Left,Top,Right和Bottom不是依賴項屬性,因此無法使用DoubleAnimation進行動畫處理允許補間)。
是做什麼用動畫的保證金是ObjectAnimation這不補。這就是爲什麼你會看到保證金從原來的位置跳轉到新的位置。作爲另一個常見示例,當您嘗試在Visible和Collapsed之間設置Visibility屬性的動畫效果時會發生同樣的情況。
你要麼需要做的基於定時器的動畫以動畫保證金或實現自己的動畫類型厚度的對象。
3
0
Here is an updated version,讓您從內XAML
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace NiceCards.Animations
{
public class ThicknessAnimationX
{
public static readonly DependencyProperty ElementProperty = DependencyProperty.RegisterAttached("Element", typeof(DependencyObject), typeof(DoubleAnimation), new PropertyMetadata(new PropertyChangedCallback(OnElementPropertyChanged)));
// The time along the animation from 0-1
public static DependencyProperty TimeProperty = DependencyProperty.RegisterAttached("Time", typeof(double), typeof(DoubleAnimation), new PropertyMetadata(OnTimeChanged));
// The object being animated
public static DependencyProperty TargetProperty = DependencyProperty.RegisterAttached("Target", typeof(DependencyObject), typeof(ThicknessAnimationX), null);
public static DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached("TargetProperty", typeof(DependencyProperty), typeof(DependencyObject), null);
public static readonly DependencyProperty FromProperty = DependencyProperty.RegisterAttached("From", typeof(Thickness), typeof(DoubleAnimation), null);
public static readonly DependencyProperty ToProperty = DependencyProperty.RegisterAttached("To", typeof(Thickness), typeof(DoubleAnimation), null);
public static void SetElement(DependencyObject o, DependencyObject value)
{
o.SetValue(ElementProperty, value);
}
public static DependencyObject GetElement(DependencyObject o)
{
return (DependencyObject)o.GetValue(ElementProperty);
}
private static void OnElementPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null)
{
DoubleAnimation doubleAnimation = (DoubleAnimation)d;
doubleAnimation.SetValue(TargetProperty, e.NewValue);
doubleAnimation.From = 0;
doubleAnimation.To = 1;
doubleAnimation.SetValue(TargetPropertyProperty, FrameworkElement.MarginProperty);
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("(ThicknessAnimationX.Time)"));
Storyboard.SetTarget(doubleAnimation, doubleAnimation);
}
}
private static void OnTimeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
DoubleAnimation animation = (DoubleAnimation)sender;
double time = GetTime(animation);
Thickness from = (Thickness)sender.GetValue(FromProperty);
Thickness to = (Thickness)sender.GetValue(ToProperty);
DependencyProperty targetProperty = (DependencyProperty)sender.GetValue(TargetPropertyProperty);
DependencyObject target = (DependencyObject)sender.GetValue(TargetProperty);
target.SetValue(targetProperty, new Thickness((to.Left - from.Left) * time + from.Left,
(to.Top - from.Top) * time + from.Top,
(to.Right - from.Right) * time + from.Right,
(to.Bottom - from.Bottom) * time + from.Bottom));
}
public static double GetTime(DoubleAnimation animation)
{
return (double)animation.GetValue(TimeProperty);
}
public static void SetTime(DoubleAnimation animation, double value)
{
animation.SetValue(TimeProperty, value);
}
public static Thickness GetFrom(DoubleAnimation animation)
{
return (Thickness)animation.GetValue(FromProperty);
}
public static void SetFrom(DoubleAnimation animation, Thickness value)
{
animation.SetValue(FromProperty, value);
}
public static Thickness GetTo(DoubleAnimation animation)
{
return (Thickness)animation.GetValue(ToProperty);
}
public static void SetTo(DoubleAnimation animation, Thickness value)
{
animation.SetValue(ToProperty, value);
}
}
}
動畫然後你就可以做到這一點在XAML
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Positions">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.2"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Left">
<Storyboard>
<DoubleAnimation Duration="0:0:0.3" NiceCards:ThicknessAnimationX.To="0,0,0,0" NiceCards:ThicknessAnimationX.Element="{Binding ElementName=rectangle1}" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Right">
<Storyboard>
<DoubleAnimation Duration="0:0:0.3" NiceCards:ThicknessAnimationX.To="0,200,0,0" NiceCards:ThicknessAnimationX.Element="{Binding ElementName=rectangle1}" Storyboard.TargetName="rectangle1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle Height="100" HorizontalAlignment="Left" Margin="23,25,0,0" x:Name="rectangle1" Stroke="Black" StrokeThickness="1" VerticalAlignment="Top" Width="200" Fill="#FF1BAA00"/>
注意,如果您不要將Target屬性設置爲XAML中的DoubleAnimation,您將無法在Blend中顯示控件/頁面。爲了解決這個問題,只需添加一個假目標屬性(在上面的代碼我增加了透明度屬性,它是一個雙值),它會在運行時無論如何將覆蓋
相關問題
- 1. 保證金動畫?
- 2. 動畫保證金/厚度
- 3. 保證金變化事件
- 4. Silverlight和數據綁定屬性動畫(保證金)
- 5. jQuery的動畫保證金頂
- 6. JQuery的動畫使用保證金
- 7. jQuery動畫和CSS保證金
- 8. 保證金不會動畫返回0?
- 9. WPF刪除保證金動畫編程
- 10. 撥動保證金
- 11. 保證金自動變爲負值
- 12. 保證金:自動自動;
- 13. 無法改變保證金
- 14. 改變保證金:無
- 15. 位置div保證金頂部30px,但保留保證金在中心使用保證金自動?
- 16. 動態保證金在窗口拖動
- 17. 帶有自動保證金的網格 - Windows Phone 8.1 Silverlight
- 18. CSS - 保證金對保證金
- 19. jQuery動畫失敗,保證金 - 左/右自動
- 20. CSS3不使用保證金的振動動畫
- 21. 改變保證金編程在WPF/C#
- 22. 浮動和保證金
- 23. CSS動態保證金?
- 24. CSS保證金自動
- 25. 保證金自動夢魘
- 26. 保證金:汽車自動
- 27. 保證金在wrappanel
- 28. 保證金左:自動在IE6和IE7
- 29. 保證金在一個div與浮動
- 30. Bootstrap Row在保證金後面啓動
我試圖在XAML中使用這個按你的例如,我在SL5中遇到了一系列錯誤。我已經添加了一個'的xmlns:someName'聲明,但它似乎XAML不知道什麼是'someName:ThicknessAnimationX'屬性。 – Shaamaan 2014-01-15 09:02:57