Question

I'm trying to create an animated control which changes opacity based on cue (pressing space).

The animation itself works properly. Based on the keypress, the opacity animates and the control comes into view. I put the animation on a grid control which contains the rest of the content.

The problem is this: After the animation finishes, the IsEnabled property of the grid remains "false", even after I manually (from code-behind) set it to true. That command doesn't raise an exception, but doesn't actually change the property either.

When I then run the same animation again, but reverse, the value actually does become "true". But of course, at that moment it is already fading out.

void Initialize()
{
    // fade animation storyboard
    FadeAnimation = new DoubleAnimation(0.0, 1.0, 
        new Duration(TimeSpan.FromSeconds(0.7)))
    { FillBehavior = FillBehavior.HoldEnd };

    FadeStoryboard.Children.Add(FadeAnimation);
    Storyboard.SetTargetName(FadeAnimation, grid.Name);
    Storyboard.SetTargetProperty(FadeAnimation, 
    new PropertyPath(Grid.OpacityProperty));
}


// flipping the animation around
void InputManager_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space)
    {
        if (!ViewModel.IsActive)
        {
            FadeAnimation.From = grid.Opacity;
            FadeAnimation.To = 1.0;
            FadeStoryboard.Begin(this);
            ViewModel.IsActive = true;
        }
        else if (ViewModel.IsActive)
        {
            FadeAnimation.From = grid.Opacity;
            FadeAnimation.To = 0.0;
            FadeStoryboard.Begin(this);
            ViewModel.IsActive = false;
        }
    }
}

[Edit] Through some additional debugging I've found that it's the animation that actively sets enabled to false on fade-in, and sets it to true when it fades out, which is the exact opposite of what I want it to do.

[Edit 2] I'm getting the exact same problem when I ditch the animation altogether, and simply change the visibility to and from Hidden. It enables when I make it Hidden, and disables when I make it Visible.

Was it helpful?

Solution 2

It turned out to be a problem with the viewmodels. To be more precise: a higher level viewmodel was simultaneously disabling a higher level control. That's why it was impossible to enable this lower level one.

OTHER TIPS

DependencyProperties have a precedence associated to them, in which animations are high up on the list.

  • Property system coercion
  • Active animations, or animations with a Hold behavior.
  • Local value
  • TemplatedParent template properties
  • Implicit style
  • Style triggers
  • Template triggers
  • Style setters
  • Default (theme) style
  • Inherited from parent
  • Default value from dependency property metadata

By default, animations have a FillBehavior of HoldEnd, which means that they stay at the value that the animation ended at. So in your case, you're trying to change that value of IsEnabled, but it's being held at a higher level of precedence. Here's what you can do solve it:

Add an ExitAction to your IsEnabled trigger to stop the storyboard, preventing the animation from continuing to assert the value it had at the end of the animation so that the local value style can be applied. This option has the benefits of not having to repeat the style (as in #1) while also not having to reverse the animation (as in #2).

More info and links for back up information can be found at here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top