using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
     
    namespace FadePanel
    {
     class FadeStackPanel:StackPanel
     {
     DoubleAnimation fadeOut = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromMilliseconds(1000)));
     DoubleAnimation fadeIn = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromMilliseconds(1000)));
     
     public FadeStackPanel ():base()
     {
     }
     protected override void OnRenderSizeChanged (System.Windows.SizeChangedInfo sizeInfo)
     {
     base.OnRenderSizeChanged(sizeInfo);
     int totalChildren = base.VisualChildrenCount;
     //Enumerates stackpanel children
     for (int i = 0; i < totalChildren; i++)
     {
     UIElement child = base.Children[i];
     FrameworkElement elem=child as FrameworkElement;
     if (elem != null)
     {
     bool outside = (child.DesiredSize.Width < child.RenderSize.Width);
     if (outside)
     {
     if (elem.Tag == null) elem.BeginAnimation(UIElement.OpacityProperty, fadeOut);
     elem.Tag = 0;
     }
     else
     if (elem.Tag != null)
     {
     child.BeginAnimation(UIElement.OpacityProperty, fadeIn);
     elem.Tag = null;
     }
     }
     }
     }
     }
    }