Thrill Science
Thrill Science

Reputation: 408

How do I animate an Image ScaleTransform from C# in a WPF program?

I want to animate this ScaleTransform

       <Image x:Name="photoB" Height="290"  Width="420" Stretch="Uniform" RenderTransformOrigin="0.5,0.5" >
             <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleY="40" ScaleX="40"/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
       </Image>

The problem is I can't find a TargetProperty that makes WPF happy!

I've tried things like

Storyboard.SetTargetProperty(kenBurnsAnimationA, new PropertyPath("(Image.RenderTransform)[0].ScaleTransform.ScaleX"));

and

Storyboard.SetTargetProperty(kenBurnsAnimationA, new PropertyPath("ScaleTransform.ScaleX"));

and

Storyboard.SetTargetProperty(kenBurnsAnimationA, new PropertyPath("(Image.RenderTransform.TransformGroup.ScaleTransform.ScaleX"));

but nothing seems to work.

What's the secret for C# "CodeBehind" of XAML?

Upvotes: 0

Views: 2659

Answers (2)

Thrill Science
Thrill Science

Reputation: 408

The problem was that I needed to add an empty Image.RenderTransform TransformGroup to my XAML

       <Image x:Name="photoA" Height="290"  Width="420" Stretch="Uniform" RenderTransformOrigin="0.5,0.5" >
            <Image.RenderTransform>
                <TransformGroup>
                    <ScaleTransform />
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Image.RenderTransform>
        </Image>

You can't animate these if they don't exist! I suppose I could have added the transfer group from C# as well.

Thanks @Anton Tykhyy for pointing me to the corrrect path.

Upvotes: 0

Anton Tykhyy
Anton Tykhyy

Reputation: 20086

Try (Image.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX).

Upvotes: 1

Related Questions