Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. WPF storyboard and animation

WPF storyboard and animation

Scheduled Pinned Locked Moved WPF
wpfcsharptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    Hi I am trying to create and start an animation in code instead of using xaml. When I use the Storyboard object(second example) nothing happens(There are no visible results of the animation), if I set animation's properties and start it without using the Storyboard object everything works normaly(first example). Below is the sample code which is put in the event handler for a button which is on the UserControl. //This works normaly btn_Click(object sender, RoutedEventArgs e) { LinearGradientBrush mask = new LinearGradientBrush(); GradientStop transparentStop = new GradientStop(Colors.Transparent, 0); GradientStop visibleStop = new GradientStop(Colors.Black, 0); mask.GradientStops.Add(transparentStop); mask.GradientStops.Add(visibleStop); this.OpacityMask = mask; DoubleAnimation visibleStopAnimation = new DoubleAnimation(0, 1.2, TimeSpan.FromSeconds(1.2), FillBehavior.HoldEnd); visibleStop.BeginAnimation(GradientStop.OffsetProperty, visibleStopAnimation); } //This doesn't work btn_Click(object sender, RoutedEventArgs e) { LinearGradientBrush mask = new LinearGradientBrush(); GradientStop transparentStop = new GradientStop(Colors.Transparent, 0); GradientStop visibleStop = new GradientStop(Colors.Black, 0); mask.GradientStops.Add(transparentStop); mask.GradientStops.Add(visibleStop); this.OpacityMask = mask; Storyboard story = new Storyboard(); story.Duration = TimeSpan.FromSeconds(1); DoubleAnimation visibleStopAnimation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd); Storyboard.SetTarget(visibleStopAnimation, visibleStop); Storyboard.SetTargetProperty(visibleStopAnimation, new PropertyPath("(Offset)")); story.Children.Add(visibleStopAnimation); story.Begin(); } Does anybody has any idea? Any advice will be appreciated! Uros

    S 1 Reply Last reply
    0
    • K koleraba

      Hi I am trying to create and start an animation in code instead of using xaml. When I use the Storyboard object(second example) nothing happens(There are no visible results of the animation), if I set animation's properties and start it without using the Storyboard object everything works normaly(first example). Below is the sample code which is put in the event handler for a button which is on the UserControl. //This works normaly btn_Click(object sender, RoutedEventArgs e) { LinearGradientBrush mask = new LinearGradientBrush(); GradientStop transparentStop = new GradientStop(Colors.Transparent, 0); GradientStop visibleStop = new GradientStop(Colors.Black, 0); mask.GradientStops.Add(transparentStop); mask.GradientStops.Add(visibleStop); this.OpacityMask = mask; DoubleAnimation visibleStopAnimation = new DoubleAnimation(0, 1.2, TimeSpan.FromSeconds(1.2), FillBehavior.HoldEnd); visibleStop.BeginAnimation(GradientStop.OffsetProperty, visibleStopAnimation); } //This doesn't work btn_Click(object sender, RoutedEventArgs e) { LinearGradientBrush mask = new LinearGradientBrush(); GradientStop transparentStop = new GradientStop(Colors.Transparent, 0); GradientStop visibleStop = new GradientStop(Colors.Black, 0); mask.GradientStops.Add(transparentStop); mask.GradientStops.Add(visibleStop); this.OpacityMask = mask; Storyboard story = new Storyboard(); story.Duration = TimeSpan.FromSeconds(1); DoubleAnimation visibleStopAnimation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd); Storyboard.SetTarget(visibleStopAnimation, visibleStop); Storyboard.SetTargetProperty(visibleStopAnimation, new PropertyPath("(Offset)")); story.Children.Add(visibleStopAnimation); story.Begin(); } Does anybody has any idea? Any advice will be appreciated! Uros

      S Offline
      S Offline
      sivaddrahcir
      wrote on last edited by
      #2

      When you work with Storyboards in code you need to use RegisterName. You can learn more about the "why" here: Storyboards Overview (MSDN)[^]

      LinearGradientBrush mask = new LinearGradientBrush();
      GradientStop transparentStop = new GradientStop(Colors.Transparent, 0);
      GradientStop visibleStop = new GradientStop(Colors.Black, 0);
      mask.GradientStops.Add(transparentStop);
      mask.GradientStops.Add(visibleStop);

      this.OpacityMask = mask;

      NameScope.SetNameScope(this, new NameScope());
      this.RegisterName("visibleGradientStop", visibleStop);

      DoubleAnimation visibleStopAnimation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd);

      Storyboard story = new Storyboard();
      story.Duration = TimeSpan.FromSeconds(1);
      story.Children.Add(visibleStopAnimation);

      Storyboard.SetTargetName(visibleStopAnimation, "visibleGradientStop");
      Storyboard.SetTargetProperty(visibleStopAnimation, new PropertyPath(GradientStop.OffsetProperty));

      story.Begin(this);

      Blog: http://windowsclientdevelopment.spaces.live.com FAQs: http://windowspresentationfoundation.wikispaces.com http://windowsmobile.wikispaces.com http://vsto.wikispaces.com

      K 1 Reply Last reply
      0
      • S sivaddrahcir

        When you work with Storyboards in code you need to use RegisterName. You can learn more about the "why" here: Storyboards Overview (MSDN)[^]

        LinearGradientBrush mask = new LinearGradientBrush();
        GradientStop transparentStop = new GradientStop(Colors.Transparent, 0);
        GradientStop visibleStop = new GradientStop(Colors.Black, 0);
        mask.GradientStops.Add(transparentStop);
        mask.GradientStops.Add(visibleStop);

        this.OpacityMask = mask;

        NameScope.SetNameScope(this, new NameScope());
        this.RegisterName("visibleGradientStop", visibleStop);

        DoubleAnimation visibleStopAnimation = new DoubleAnimation(0, 1, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd);

        Storyboard story = new Storyboard();
        story.Duration = TimeSpan.FromSeconds(1);
        story.Children.Add(visibleStopAnimation);

        Storyboard.SetTargetName(visibleStopAnimation, "visibleGradientStop");
        Storyboard.SetTargetProperty(visibleStopAnimation, new PropertyPath(GradientStop.OffsetProperty));

        story.Begin(this);

        Blog: http://windowsclientdevelopment.spaces.live.com FAQs: http://windowspresentationfoundation.wikispaces.com http://windowsmobile.wikispaces.com http://vsto.wikispaces.com

        K Offline
        K Offline
        koleraba
        wrote on last edited by
        #3

        Thank you gurge60, that solved my problem.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups