简体   繁体   中英

In WPF I can't see whats wrong with my C# Storyboard and DoubleAnimation

Im attempting to create a storyboard in C# not XAML to control scaling of an image, so i can easily alter the ScaleTransform.ScaleX and ScaleTransform.ScaleY values in a DoubleAnimation .

So far I believe i have created the animations and added it to a new storyboard, and the appropriate values change in the C# when i check with breakpoints, but it's not actually working.

My C# looks like this:

    public void SetStatistics(double[] value)
    {
        Storyboard sb = new Storyboard();
        sb.Duration = new Duration(TimeSpan.FromSeconds(1));

        //Wedge Animation X-Axis    
        DoubleAnimation wax = new DoubleAnimation();
        //Wedge Animation Y-Axis  
        DoubleAnimation way = new DoubleAnimation();

        ScaleTransform st = ((ScaleTransform)FindName("wedge1scale"));

        wax = new DoubleAnimation();
        way = new DoubleAnimation();
        wax.Duration = sb.Duration;
        way.Duration = sb.Duration;

        sb.Children.Add(wax);
        sb.Children.Add(way);

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

        //End scale from calculation with an Enum value
        wax.To = StatMin + (StatPercent * value[1]);
        //Start scale from current value
        wax.From = st.ScaleX;

        Storyboard.SetTargetProperty(way, new PropertyPath("(ScaleTransform.ScaleY)"));

        //End scale from calculation with an Enum value
        way.To = StatMin + (StatPercent * value[1]);
        //Start scale from current value
        way.From = st.ScaleY;

        Storyboard.SetTarget(wax, Wedge1);
        Storyboard.SetTarget(way, Wedge1);

        Main.Resources.Add("animation", sb);

        sb.Begin();
    }

My XAML Image is like this:

    <Image x:Name="Wedge1" Source="Images/Wedge.png" RenderTransformOrigin="-0.008,1.027" Height="682" Width="263" Canvas.Left="869.04" Canvas.Top="-158.251" >

        <Image.RenderTransform>
              <TransformGroup>
                    <ScaleTransform x:Name="wedge1scale" ScaleX="0.555" ScaleY="0.555"/>
                    <TranslateTransform X="88.102" Y="-4.381"/>
              </TransformGroup>
        </Image.RenderTransform>
    </Image>

Thanks in advance for any info :)

The problem is your PropertyPath . You would have to write RenderTransform.Children[0].ScaleX and RenderTransform.Children[0].ScaleY in order to animate the ScaleX and ScaleY properties of the first child of the TransformGroup in the Image's RenderTransform .

var wax = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };
var way = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };

wax.To = ...
way.To = ...

Storyboard.SetTargetProperty(
    wax, new PropertyPath("RenderTransform.Children[0].ScaleX"));
Storyboard.SetTargetProperty(
    way, new PropertyPath("RenderTransform.Children[0].ScaleY"));

Storyboard.SetTarget(wax, Wedge1);
Storyboard.SetTarget(way, Wedge1);

var sb = new Storyboard();
sb.Children.Add(wax);
sb.Children.Add(way);
sb.Begin();

And it would be less code without the Storyboard:

var wax = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };
var way = new DoubleAnimation { Duration = TimeSpan.FromSeconds(1) };

wax.To = ...
way.To = ...

wedge1scale.BeginAnimation(ScaleTransform.ScaleXProperty, wax);
wedge1scale.BeginAnimation(ScaleTransform.ScaleYProperty, way);

And I guess in your case it isn't necessary to set the From property, as the animations start from the current property values by default.

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