简体   繁体   中英

Simultanious Animation of Elements in WPF

I have a question according WPF animation in 3D.

In my user control, there are a dynamic number of wpf elements that are created in code behind. They are positioned circularly around the y-axis.

I create each element with an initial angle:

var trans = new AxisAngleRotation3D();
trans.Axis = new Vector3D(0, 1, 0);
trans.Angle = initialAngleFromZero;
RotateTransform3D elementTransform = new RotateTransform3D(trans);
wpfElement.Transform = elementTransform;

Each element is then displayed as desired.

Now, I want to rotate all of the dynamically created objects around the y-axis by the same angle at the same time in code behind. How do I that? I have tried the following:

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();
}

But this does not work, nothing happens.

To do a group animation like that, or to animate multiple objects at once, you might find that you have more success by developing a custom Panel ... you can build the animation right into it and once developed, you just set it as the ItemsPanel for a UI collection control and add items to see it animate. I have several and use them like this:

<ListBox ItemSource="{Binding SomeCollection}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <Controls:AnimatedRotationPanel EntryAnimationType="Slide" 
EntryAnimationDirection="Up" HorizontalContentAlignment="Stretch" 
ExitAnimationType="Slide" ExitAnimationDirection="Down" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

As you can see, I added extra properties to the Panel to provide more options when using it, but you can make yours as simple or complicated as you want. To help you out with developing these Panel s, please take a look at the following links and please don't get put off by what at first, seems like some challenging code:

Panels Overview from MSDN
How to create a Custom Layout Panel in WPF on WPF Tutorial.NET
Creating Custom Panels In WPF from CodeProject
Animated WPF Panels from CodeProject

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM