Silverlightで境界のサイズ変更をアニメーション化していますが、周りのマージンも徐々に削除する必要があります(現在50)。ブレンドはマージンの変更のためのトゥイーンを生成しないようです - 一度に50から0にジャンプします。これを達成する方法はありますか?Silverlightの余白のマージンを変更する
7
A
答えて
8
問題は、Marginは実際に依存オブジェクトではないタイプのSystem.Windows.Thicknessであるため、Left、Top、Right、およびBottomは依存性プロパティではないため、DoubleAnimationを使用してアニメーション化することはできません。トゥイーンを許可する)。
マージンをアニメートするために使用されるものは、トゥイーンではないObjectAnimationです。これが、元の場所から新しい場所へのマージンジャンプを見る理由です。別の一般的な例として、VisibilityとCollapsedの間でVisibilityプロパティをアニメートしようとすると同じことが起こります。
あなたはどちらかのマージンをアニメーションや厚さのオブジェクトのための独自のアニメーションの種類を実装するためにタイマーベースのアニメーションを実行する必要があります。
3
ベン・レモンは、エレガントなソリューション提供します:http://blogs.msdn.com/blemmon/archive/2009/03/18/animating-margins-in-silverlight.aspx
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"/>
注場合は、そのXAMLでTargetプロパティをDoubleAnimationに設定しないと、Blendでコントロール/ページを表示できなくなります。これを修正するには、偽のターゲットプロパティを追加してください(上のコードでは、double値の不透明度プロパティを追加しました)。実行時にオーバーライドされます。
関連する問題
- 1. パディングのマージン、余白
- 2. HTMLの余白とマージン?
- 3. LyXのページ余白を変更する
- 4. マージンを中心とした要素上の余白マージン:0 auto;
- 5. CSS - 親の余白を削除するための負のマージン
- 6. プログラムでキャンバスの余白を変更する方法
- 7. gvimの左余白を変更するには
- 8. 下余白付きのCSS自動水平マージン
- 9. ボタンの余白 - 左余白左の画像の左余白
- 10. フレックスボックスは、次のHTMLを考えると子供のマージン(余白崩壊)
- 11. 余白を変更する方法コード別にトップ?
- 12. スパン、余分なマージン
- 13. ButtonTextのマージン/パディングを変更する
- 14. リストビューの余白
- 15. ウィンドウのサイズ変更後に描画の余白を維持する
- 16. divのサイズを変更した後の余白のサイズ変更を停止する方法
- 17. Android - EditTextフローティングラベルの余白/余白を減らしますか?
- 18. 余白を維持してプログラムでUIButtonのサイズを変更する
- 19. Googleチャットテーブル内のセルに余白または余白を追加
- 20. コンテナのdivの余分なマージンIE6
- 21. HTMLページの余分な余白のソース?
- 22. ggplotで余白を取り除き、ラベルのフォントスタイルを変更します
- 23. インラインリストの余白または余白の使用
- 24. 余白が変更されたときにレイアウトのサイズを変更しないようにする
- 25. 画面のサイズを変更するとき、左の余白は同じで、右の余白は縮小されます
- 26. 予期しない画像リンク内の余白/余白
- 27. 実行時のAndroidウィジェットの変更マージン
- 28. WPFのTreeView子の余白
- 29. ラテックスの本クラスの余白
- 30. jsqmessageviewcontrollerのcustomMediaItemの余白
これをXAMLで使用しようとしました私はSL5でエラーのストリームを取得しています。私は 'xmlns:someName'宣言を追加しましたが、XAMLは' someName:ThicknessAnimationX'プロパティが何であるか分からないようです。 – Shaamaan