And now I am stuck again, this time animating scale

Tre4b 136 Reputation points
2020-12-16T21:08:12.403+00:00

This is the guts of my function to take an image and scale it, but nothing happens. I thought I had tracked down why with including RenderTransform in the PropertyPath but that made no difference. Everything seems to suggest that this should work. What am I not seeing here?

        Storyboard sb = new Storyboard();
        DoubleAnimation ScaleAnimation = new DoubleAnimation();
        ScaleAnimation.Duration = TimeSpan.FromMilliseconds(milliseconds);
        ScaleAnimation.DecelerationRatio = 0.9f;
        ScaleAnimation.From = 1.0;
        ScaleAnimation.To = 10.0;

        Storyboard.SetTarget(ScaleAnimation, MainImage);
        Storyboard.SetTargetProperty(ScaleAnimation, new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));
        sb.Children.Add(ScaleAnimation);
        sb.Begin();

Xaml is reall basic and looks like this

<Canvas Name="DisplaySurface" >
    <Image x:Name="MainImage" HorizontalAlignment="Center" Canvas.Left="0" Canvas.Top="0" Width="{Binding ActualWidth, ElementName=DisplaySurface}" Height="{Binding ActualHeight, ElementName=DisplaySurface}">
        <Image.RenderTransform>
            <TransformGroup>
                <ScaleTransform x:Name="ImageScale"/>
            </TransformGroup>
        </Image.RenderTransform>
    </Image>
    <Button HorizontalAlignment="Left" VerticalAlignment="Top" Content="Button" Canvas.Left="66" Canvas.Top="50" Width="75" Click="Button_Click"/>
</Canvas>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,669 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-12-17T05:58:14.21+00:00

    Try to add RenderTransformOrigin and initialize a new instance of the ScaleTransform class for MainImage. The below is my updated code for your:

         //Add below two lines code  
            MainImage.RenderTransform = new ScaleTransform();  
            MainImage.RenderTransformOrigin = new Point(0.5, 0.5);  
    
            Storyboard sb = new Storyboard();  
            DoubleAnimation ScaleAnimation = new DoubleAnimation();  
            ScaleAnimation.Duration = TimeSpan.FromMilliseconds(milliseconds);  
            ScaleAnimation.DecelerationRatio = 0.9f;  
            ScaleAnimation.From = 1.0;  
            ScaleAnimation.To = 10.0;  
            Storyboard.SetTarget(ScaleAnimation, MainImage);  
            Storyboard.SetTargetProperty(ScaleAnimation, new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));  
            sb.Children.Add(ScaleAnimation);  
            sb.Begin();  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful