我需要COLLAPSED
通过 C# 为属性设置动画。
我有下面的代码,除了没有折叠动画外,它几乎可以正常工作。
有什么线索吗?
var myElement = stackObj.Children[n];
Duration d = TimeSpan.FromSeconds(2);
Storyboard sb = new Storyboard() { Duration = d };
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
ObjectAnimationUsingKeyFrames objectAnimationUsingKeyFrames = new ObjectAnimationUsingKeyFrames();
var discreteObjectKeyFrame = new DiscreteObjectKeyFrame()
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(2))
};
objectAnimationUsingKeyFrames.KeyFrames.Add(discreteObjectKeyFrame);
Storyboard.SetTarget(objectAnimationUsingKeyFrames, myElement);
Storyboard.SetTargetProperty(objectAnimationUsingKeyFrames, new PropertyPath("Visibility.Collapsed"));
sb.Children.Add(DA);
string myObjectName = "r" + n;
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
sb.Begin(this);
n++;
我知道在 XAML 中应该是这样的
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty = "Opacity"
To = "0"
BeginTime = "0:0:0"
Duration = "0:0:2" />
<ObjectAnimationUsingKeyFrames
Storyboard.TargetProperty = "Visibility">
<DiscreteObjectKeyFrame KeyTime="0:0:2" Value="{x:Static Visibility.Collapsed}" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
但我不知道我必须实现哪个 C# 代码。