简体   繁体   中英

WPF enlarge a rectangle using storyboard

Hi i have a Window>grid>rectangle named as (rect1)

how do i enlarge this using storyboard

Error:Additional information: No applicable name scope exists to resolve the name 'rect1'

private void Window_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {

        Storyboard buttonEnlargeStoryboard = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();
        da.SetValue(Storyboard.TargetNameProperty, rect1.Name);
        da.BeginTime = new TimeSpan(0);
        da.Duration = TimeSpan.FromSeconds(1);
        buttonEnlargeStoryboard.Children.Add(da);

        buttonEnlargeStoryboard.Begin();



    }

You should animate width and height properties like this:

        DoubleAnimation widthAnimation = new DoubleAnimation
        {
            From = 0,
            To = rect1.ActualWidth*2,
            Duration = TimeSpan.FromSeconds(5)
        };

        DoubleAnimation heightAnimation = new DoubleAnimation
        {
            From = 0,
            To = rect1.ActualHeight*2,
            Duration = TimeSpan.FromSeconds(5)
        };

        Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(Rectangle.WidthProperty));
        Storyboard.SetTarget(widthAnimation, rect1);

        Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(Rectangle.HeightProperty));
        Storyboard.SetTarget(heightAnimation, rect1);

        Storyboard buttonEnlargeStoryboard = new Storyboard();
        buttonEnlargeStoryboard.SpeedRatio = 1;
        buttonEnlargeStoryboard.Children.Add(widthAnimation);
        buttonEnlargeStoryboard.Children.Add(heightAnimation);
        buttonEnlargeStoryboard.Begin();

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