C# WPF中元素的同时动画
我有一个关于3D WPF动画的问题 在我的用户控件中,在代码隐藏中创建了动态数量的wpf元素。它们围绕y轴循环放置 我使用初始角度创建每个元素:C# WPF中元素的同时动画,c#,wpf,animation,user-interface,rotation,C#,Wpf,Animation,User Interface,Rotation,我有一个关于3D WPF动画的问题 在我的用户控件中,在代码隐藏中创建了动态数量的wpf元素。它们围绕y轴循环放置 我使用初始角度创建每个元素: var trans = new AxisAngleRotation3D(); trans.Axis = new Vector3D(0, 1, 0); trans.Angle = initialAngleFromZero; RotateTransform3D elementTransform = new RotateTransform3D(trans);
var trans = new AxisAngleRotation3D();
trans.Axis = new Vector3D(0, 1, 0);
trans.Angle = initialAngleFromZero;
RotateTransform3D elementTransform = new RotateTransform3D(trans);
wpfElement.Transform = elementTransform;
然后根据需要显示每个元素
现在,我想在代码隐藏中以相同的角度同时围绕y轴旋转所有动态创建的对象。我该怎么做?我尝试了以下方法:
foreach(var wpfElement in wpfElements)
{
var sb = new Storyboard();
var ani = new DoubleAnimation();
ani.Duration = new Duration(TimeSpan.FromSeconds(5));
ani.From = fromAngle;
ani.To = toAngle;
sb.Children.Add(ani);
Storyboard.SetTarget(ani, wpfElement.Transform);
Storyboard.SetTargetProperty(ani, new PropertyPath(AxisAngleRotation3D.AngleProperty));
sb.Begin();
}
但是这不起作用,什么也不会发生。要进行那样的成组动画,或者同时对多个对象进行动画制作,您可能会发现,通过开发自定义的
面板
,您会获得更大的成功。。。您可以将动画直接构建到其中,一旦开发完成,您只需将其设置为UI集合控件的ItemsPanel
,并添加项目以查看其动画效果。我有几个,使用方式如下:
<ListBox ItemSource="{Binding SomeCollection}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Controls:AnimatedRotationPanel EntryAnimationType="Slide"
EntryAnimationDirection="Up" HorizontalContentAlignment="Stretch"
ExitAnimationType="Slide" ExitAnimationDirection="Down" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
如您所见,我在面板中添加了额外的属性,以便在使用时提供更多选项,但您可以根据需要将属性设置为简单或复杂。为了帮助您开发这些面板
s,请查看以下链接,请不要因为一开始似乎是一些具有挑战性的代码而感到不快:
来自MSDN
在WPF Tutorial.NET上
来自CodeProject
来自CodeProject