Rajat Saxena
Rajat Saxena

Reputation: 3925

C# equivalent for xaml code for animation

I created this little animation using expression blend for a rectangle named "rect"

<Storyboard x:Name="flipanim">
            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)" Storyboard.TargetName="rect">
                <EasingDoubleKeyFrame KeyTime="0" Value="90"/>
                <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>

Now I want to show this animation for every listbox item(generated using itemtemplate) loaded at runtime,How can I set listitems' animate property.How can I specify TargetName property for list items? And if that's not possible then I would like to know about how to convert the above code in C#.

Upvotes: 0

Views: 236

Answers (2)

Jaihind
Jaihind

Reputation: 2778

May this will help you

DoubleAnimation rotation = new DoubleAnimation();
            rotation.From = 0;
            rotation.To = 90;
            rotation.Duration = new Duration(TimeSpan.FromSeconds(0.5));
            Storyboard.SetTarget(rotation, rect);
            Storyboard.SetTargetProperty(rotation, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)"));
            Storyboard flipanim= new Storyboard();
            flipanim.Children.Add(rotation);
            flipanim.Begin();

Upvotes: 1

atomaras
atomaras

Reputation: 2568

You can define the Storyboard inside the ItemTemplate, so each item has it's own, and use an EventTrigger for the Loaded event and an Action to start your Storyboard. Define the Storyboard as a Resource of the first element inside your DataTemplate so you can access named elements (like here).

Upvotes: 0

Related Questions