Click here to Skip to main content
16,018,460 members
Articles / Desktop Programming / WPF

WPF: How To Animate Visibility Property?

Rate me:
Please Sign up or sign in to vote.
4.86/5 (30 votes)
7 Feb 2010Ms-PL1 min read 103K   24   20
An easy way to add fade-in / fade-out effects to your user controls, when you change their Visibility property

In this post, I’ll show you an easy way to add fade-in / fade-out effects to your user controls, when you change their Visibility property.

Adding the animation is done with an attached property, so using the code will be extremely simple.

Usage Sample

XAML:

<Window x:Class="WpfDemoVisibilityAnimation.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:common="clr-namespace:WPF.Common"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <Button Content="Hide" Click="Hide_Click" />
        <Button Content="Show" Click="Show_Click" />
        <Image common:VisibilityAnimation.AnimationType="Fade" 
               x:Name="Image" 
               Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg" />
    </StackPanel>
</Window>

Code Behind:

C#
private void Hide_Click(object sender, RoutedEventArgs e)
{
    Image.Visibility = Visibility.Hidden;
}

private void Show_Click(object sender, RoutedEventArgs e)
{
    Image.Visibility = Visibility.Visible;
}

Note that the only required addition for the animation effect was setting the attached property: VisibilityAnimation.AnimationType="Fade".

You can find here the full source code for the VisibilityAnimation attached property and here the usage sample application.

Where is the magic? The magic resides in the implementation of the attached property. Before I show you the code, let me explain the basic idea.

  1. We “register” for getting visibility property “before change” event.
  2. When an element’s visibility property tries to change, somewhere in the application, we get the notification and check if the element has our attached property.
  3. If it has, we start an animation on the opacity property and force the current visibility value to Visibility.Visible. This will allow the animation to present without interruptions.
  4. When the animation completes, we set the original requested value.
  5. Setting the original requested value will invoke (again) our “before change” event, so we need to keep a flag that indicates whether we already started the animation, in which case we just set the value.

Credit: The original idea is based on an answer from stackoverflow. I’ve improved the code, added support for the case where the Visibility was getting the value using data binding and added lots of comments.

That’s it for now,
Arik Poznanski.

Appendix A – Source Code for VisibilityAnimation Class

C#
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media.Animation;

namespace WPF.Common
{
    /// <summary>
    /// Supplies attached properties that provides visibility of animations
    /// </summary>
    public class VisibilityAnimation
    {
        public enum AnimationType
        {
            /// <summary>
            /// No animation
            /// </summary>
            None,

            /// <summary>
            /// Fade in / Fade out
            /// </summary>
            Fade
        }

        /// <summary>
        /// Animation duration
        /// </summary>
        private const int AnimationDuration = 300;

        /// <summary>
        /// List of hooked objects
        /// </summary>
        private static readonly Dictionary<FrameworkElement, bool> _hookedElements = 
            new Dictionary<FrameworkElement, bool>();

        /// <summary>
        /// Get AnimationType attached property
        /// </summary>
        /// <param name="obj">Dependency object</param>
        /// <returns>AnimationType value</returns>
        public static AnimationType GetAnimationType(DependencyObject obj)
        {
            return (AnimationType)obj.GetValue(AnimationTypeProperty);
        }

        /// <summary>
        /// Set AnimationType attached property
        /// </summary>
        /// <param name="obj">Dependency object</param>
        /// <param name="value">New value for AnimationType</param>
        public static void SetAnimationType(DependencyObject obj, AnimationType value)
        {
            obj.SetValue(AnimationTypeProperty, value);
        }

        /// <summary>
        /// Using a DependencyProperty as the backing store for AnimationType. 
        /// This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty AnimationTypeProperty = 
            DependencyProperty.RegisterAttached(
                "AnimationType",
                typeof(AnimationType),
                typeof(VisibilityAnimation),
                new FrameworkPropertyMetadata(AnimationType.None, 
                    new PropertyChangedCallback(OnAnimationTypePropertyChanged)));

        /// <summary>
        /// AnimationType property changed
        /// </summary>
        /// <param name="dependencyObject">Dependency object</param>
        /// <param name="e">e</param>
        private static void OnAnimationTypePropertyChanged(
            DependencyObject dependencyObject, 
            DependencyPropertyChangedEventArgs e)
        {
            FrameworkElement frameworkElement = dependencyObject as FrameworkElement;

            if (frameworkElement == null)
            {
                return;
            }

            // If AnimationType is set to True on this framework element, 
            if (GetAnimationType(frameworkElement) != AnimationType.None)
            {
                // Add this framework element to hooked list
                HookVisibilityChanges(frameworkElement);
            }
            else
            {
                // Otherwise, remove it from the hooked list
                UnHookVisibilityChanges(frameworkElement);
            }
        }

        /// <summary>
        /// Add framework element to list of hooked objects
        /// </summary>
        /// <param name="frameworkElement">Framework element</param>
        private static void HookVisibilityChanges(FrameworkElement frameworkElement)
        {
            _hookedElements.Add(frameworkElement, false);
        }

        /// <summary>
        /// Remove framework element from list of hooked objects
        /// </summary>
        /// <param name="frameworkElement">Framework element</param>
        private static void UnHookVisibilityChanges(FrameworkElement frameworkElement)
        {
            if (_hookedElements.ContainsKey(frameworkElement))
            {
                _hookedElements.Remove(frameworkElement);
            }
        }

        /// <summary>
        /// VisibilityAnimation static ctor
        /// </summary>
        static VisibilityAnimation()
        {
            // Here we "register" on Visibility property "before change" event
            UIElement.VisibilityProperty.AddOwner(
                typeof(FrameworkElement),
                new FrameworkPropertyMetadata(
                    Visibility.Visible, 
                    VisibilityChanged, 
                    CoerceVisibility));
        }

        /// <summary>
        /// Visibility changed
        /// </summary>
        /// <param name="dependencyObject">Dependency object</param>
        /// <param name="e">e</param>
        private static void VisibilityChanged(
            DependencyObject dependencyObject, 
            DependencyPropertyChangedEventArgs e)
        {
            // Ignore
        }

        /// <summary>
        /// Coerce visibility
        /// </summary>
        /// <param name="dependencyObject">Dependency object</param>
        /// <param name="baseValue">Base value</param>
        /// <returns>Coerced value</returns>
        private static object CoerceVisibility(
            DependencyObject dependencyObject, 
            object baseValue)
        {
            // Make sure object is a framework element
            FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
            if (frameworkElement == null)
            {
                return baseValue;
            }

            // Cast to type safe value
            Visibility visibility = (Visibility)baseValue;

            // If Visibility value hasn't change, do nothing.
            // This can happen if the Visibility property is set using data binding 
            // and the binding source has changed but the new visibility value 
            // hasn't changed.
            if (visibility == frameworkElement.Visibility)
            {
                return baseValue;
            }

            // If element is not hooked by our attached property, stop here
            if (!IsHookedElement(frameworkElement))
            {
                return baseValue;
            }

            // Update animation flag
            // If animation already started, don't restart it (otherwise, infinite loop)
            if (UpdateAnimationStartedFlag(frameworkElement))
            {
                return baseValue;
            }

            // If we get here, it means we have to start fade in or fade out animation. 
            // In any case return value of this method will be Visibility.Visible, 
            // to allow the animation.
            DoubleAnimation doubleAnimation = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromMilliseconds(AnimationDuration))
            };

            // When animation completes, set the visibility value to the requested 
            // value (baseValue)
            doubleAnimation.Completed += (sender, eventArgs) =>
            {
                if (visibility == Visibility.Visible)
                {
                    // In case we change into Visibility.Visible, the correct value 
                    // is already set, so just update the animation started flag
                    UpdateAnimationStartedFlag(frameworkElement);
                }
                else
                {
                    // This will trigger value coercion again 
                    // but UpdateAnimationStartedFlag() function will return true 
                    // this time, thus animation will not be triggered. 
                    if (BindingOperations.IsDataBound(frameworkElement, 
                        UIElement.VisibilityProperty))
                    {
                        // Set visibility using bounded value
                        Binding bindingValue = 
                            BindingOperations.GetBinding(frameworkElement, 
                                UIElement.VisibilityProperty);
                        BindingOperations.SetBinding(frameworkElement, 
                            UIElement.VisibilityProperty, bindingValue);
                    }
                    else
                    {
                        // No binding, just assign the value
                        frameworkElement.Visibility = visibility;
                    }
                }
            };

            if (visibility == Visibility.Collapsed || visibility == Visibility.Hidden)
            {
                // Fade out by animating opacity
                doubleAnimation.From = 1.0;
                doubleAnimation.To = 0.0;
            }
            else
            {
                // Fade in by animating opacity
                doubleAnimation.From = 0.0;
                doubleAnimation.To = 1.0;
            }

            // Start animation
            frameworkElement.BeginAnimation(UIElement.OpacityProperty, doubleAnimation);

            // Make sure the element remains visible during the animation
            // The original requested value will be set in the completed event of 
            // the animation
            return Visibility.Visible;
        }

        /// <summary>
        /// Check if framework element is hooked with AnimationType property
        /// </summary>
        /// <param name="frameworkElement">Framework element to check</param>
        /// <returns>Is the framework element hooked?</returns>
        private static bool IsHookedElement(FrameworkElement frameworkElement)
        {
            return _hookedElements.ContainsKey(frameworkElement);
        }

        /// <summary>
        /// Update animation started flag or a given framework element
        /// </summary>
        /// <param name="frameworkElement">Given framework element</param>
        /// <returns>Old value of animation started flag</returns>
        private static bool UpdateAnimationStartedFlag(FrameworkElement frameworkElement)
        {
            bool animationStarted = (bool)_hookedElements[frameworkElement];
            _hookedElements[frameworkElement] = !animationStarted;

            return animationStarted;
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) Verint
Israel Israel
Arik Poznanski is a senior software developer at Verint. He completed two B.Sc. degrees in Mathematics & Computer Science, summa cum laude, from the Technion in Israel.

Arik has extensive knowledge and experience in many Microsoft technologies, including .NET with C#, WPF, Silverlight, WinForms, Interop, COM/ATL programming, C++ Win32 programming and reverse engineering (assembly, IL).

Comments and Discussions

 
QuestionSource code link not working Pin
af rey10-Aug-22 16:51
af rey10-Aug-22 16:51 
QuestionSetting new value during a animation Pin
andreas13377-May-15 6:01
andreas13377-May-15 6:01 
QuestionVisibility' property was already registered by 'FrameworkElement Pin
chrislamb8427-May-13 9:49
chrislamb8427-May-13 9:49 
AnswerRe: Visibility' property was already registered by 'FrameworkElement Pin
Member 102718467-May-14 0:29
Member 102718467-May-14 0:29 
GeneralRe: Visibility' property was already registered by 'FrameworkElement Pin
Member 1168374123-Jul-15 5:50
Member 1168374123-Jul-15 5:50 
SuggestionConsider adding a workin Solution Pin
jtstanish21-Dec-11 9:23
jtstanish21-Dec-11 9:23 
GeneralMy vote of 4 Pin
jtstanish21-Dec-11 9:21
jtstanish21-Dec-11 9:21 
GeneralBug when Visibility is data bound Pin
poulin_julien7-Apr-10 4:01
poulin_julien7-Apr-10 4:01 
GeneralRe: Bug when Visibility is data bound Pin
Arik Poznanski9-Apr-10 3:01
Arik Poznanski9-Apr-10 3:01 
GeneralRe: Bug when Visibility is data bound [modified] Pin
poulin_julien9-Apr-10 3:06
poulin_julien9-Apr-10 3:06 
GeneralA subtle bug Pin
alxxl23-Feb-10 9:54
alxxl23-Feb-10 9:54 
GeneralRe: A subtle bug Pin
Arik Poznanski26-Feb-10 9:54
Arik Poznanski26-Feb-10 9:54 
GeneralVery good post Pin
solaadio16-Feb-10 6:50
solaadio16-Feb-10 6:50 
QuestionWill it work in a WEB app? Pin
Member 305299016-Feb-10 4:19
Member 305299016-Feb-10 4:19 
AnswerRe: Will it work in a WEB app? Pin
Arik Poznanski16-Feb-10 6:56
Arik Poznanski16-Feb-10 6:56 
GeneralProblems to rebuild your example Pin
sinun16-Feb-10 1:32
sinun16-Feb-10 1:32 
GeneralRe: Problems to rebuild your example Pin
Arik Poznanski16-Feb-10 1:37
Arik Poznanski16-Feb-10 1:37 
GeneralRe: Problems to rebuild your example Pin
sinun16-Feb-10 19:52
sinun16-Feb-10 19:52 
GeneralXP Pin
gratro15-Feb-10 23:39
gratro15-Feb-10 23:39 
GeneralRe: XP Pin
Arik Poznanski16-Feb-10 1:38
Arik Poznanski16-Feb-10 1:38 
I don't see any reason why it shouldn't work on Windows XP.

Can you add some more details on why it didn't work? What are you getting?
Is there any error messages?
Arik Poznanski

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.