Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6206721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:29:55+00:00 2026-05-24T05:29:55+00:00

I’ve been using this great article as a basis for showing and hiding elements

  • 0

I’ve been using this great article as a basis for showing and hiding elements with a transition effect. It works very neatly in that it lets you bind the Visibility property just as normal, then define what happens when the visibility changes (e.g. animate its opacity or trigger a storyboard). When you hide an element, it uses value coercion to keep it visible until the transition is finished.

I’m looking for a similar solution to use with an ItemsControl and an ObservableCollection. In other words, I want to bind the ItemsSource to an ObservableCollection as normal, but control what happens when items are added and removed and trigger animations. I don’t think using value coercion will work here, but obviously, items still need to stay in the list until their transitions finish. Does anyone know of any existing solutions that would make this easy?

I’d like any solution to be reasonably generic and easy to apply to lists of any kind of items. Ideally the style and animation behaviour would be separate, and applying it to a particular list would be a simple task such as giving it an attached property.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:29:56+00:00Added an answer on May 24, 2026 at 5:29 am

    Fade-in is easy, but for fade-out the items will need to stay in the source list until the animation is completed (like you said).

    If we still want to be able to use the source ObservableCollection normally (Add/Remove etc.) then we would have to create a mirror collection that is constantly in sync with the source collection with a delay for delete until the animation is completed. This can be done with the CollectionChanged event.

    Here is an implementation I made of this, using an attached behavior. It can be used for ItemsControl, ListBox, DataGrid or anything else that derives from ItemsControl.

    Instead of Binding ItemsSource, bind the attached property ItemsSourceBehavior.ItemsSource. It will create a mirror ObservableCollection using Reflection, use the mirror as ItemsSource instead and handle the FadeIn/FadeOut animations.
    Note that I haven’t tested this extensively and there might be bugs and several improvements that can be made but it has worked great in my scenarios.

    Sample Usage

    <ListBox behaviors:ItemsSourceBehavior.ItemsSource="{Binding MyCollection}">
        <behaviors:ItemsSourceBehavior.FadeInAnimation>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                 From="0.0"
                                 To="1.0"
                                 Duration="0:0:3"/>
            </Storyboard>
        </behaviors:ItemsSourceBehavior.FadeInAnimation>
        <behaviors:ItemsSourceBehavior.FadeOutAnimation>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                 To="0.0"
                                 Duration="0:0:1"/>
            </Storyboard>
        </behaviors:ItemsSourceBehavior.FadeOutAnimation>
        <!--...-->
    </ListBox>
    

    ItemsSourceBehavior

    public class ItemsSourceBehavior
    {
        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.RegisterAttached("ItemsSource",
                                                typeof(IList),
                                                typeof(ItemsSourceBehavior),
                                                new UIPropertyMetadata(null, ItemsSourcePropertyChanged));
        public static void SetItemsSource(DependencyObject element, IList value)
        {
            element.SetValue(ItemsSourceProperty, value);
        }
        public static IList GetItemsSource(DependencyObject element)
        {
            return (IList)element.GetValue(ItemsSourceProperty);
        }
    
        private static void ItemsSourcePropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ItemsControl itemsControl = source as ItemsControl;
            IList itemsSource = e.NewValue as IList;
            if (itemsControl == null)
            {
                return;
            }
            if (itemsSource == null)
            {
                itemsControl.ItemsSource = null;
                return;
            }
    
            Type itemsSourceType = itemsSource.GetType();
            Type listType = typeof(ObservableCollection<>).MakeGenericType(itemsSourceType.GetGenericArguments()[0]);
            IList mirrorItemsSource = (IList)Activator.CreateInstance(listType);
            itemsControl.SetBinding(ItemsControl.ItemsSourceProperty, new Binding{ Source = mirrorItemsSource });
    
            foreach (object item in itemsSource)
            {
                mirrorItemsSource.Add(item);
            }
            FadeInContainers(itemsControl, itemsSource);
    
            (itemsSource as INotifyCollectionChanged).CollectionChanged += 
                (object sender, NotifyCollectionChangedEventArgs ne) =>
            {
                if (ne.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (object newItem in ne.NewItems)
                    {
                        mirrorItemsSource.Add(newItem);
                    }
                    FadeInContainers(itemsControl, ne.NewItems);
                }
                else if (ne.Action == NotifyCollectionChangedAction.Remove)
                {
                    foreach (object oldItem in ne.OldItems)
                    {
                        UIElement container = itemsControl.ItemContainerGenerator.ContainerFromItem(oldItem) as UIElement;
                        Storyboard fadeOutAnimation = GetFadeOutAnimation(itemsControl);
                        if (container != null && fadeOutAnimation != null)
                        {
                            Storyboard.SetTarget(fadeOutAnimation, container);
    
                            EventHandler onAnimationCompleted = null;
                            onAnimationCompleted = ((sender2, e2) =>
                            {
                                fadeOutAnimation.Completed -= onAnimationCompleted;
                                mirrorItemsSource.Remove(oldItem);
                            });
    
                            fadeOutAnimation.Completed += onAnimationCompleted;
                            fadeOutAnimation.Begin();
                        }
                        else
                        {
                            mirrorItemsSource.Remove(oldItem);
                        }
                    }
                }
            };
        }
    
        private static void FadeInContainers(ItemsControl itemsControl, IList newItems)
        {
            EventHandler statusChanged = null;
            statusChanged = new EventHandler(delegate
            {
                if (itemsControl.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                {
                    itemsControl.ItemContainerGenerator.StatusChanged -= statusChanged;
                    foreach (object newItem in newItems)
                    {
                        UIElement container = itemsControl.ItemContainerGenerator.ContainerFromItem(newItem) as UIElement;
                        Storyboard fadeInAnimation = GetFadeInAnimation(itemsControl);
                        if (container != null && fadeInAnimation != null)
                        {
                            Storyboard.SetTarget(fadeInAnimation, container);
                            fadeInAnimation.Begin();
                        }
                    }
                }
            });
            itemsControl.ItemContainerGenerator.StatusChanged += statusChanged;
        }
    
        public static readonly DependencyProperty FadeInAnimationProperty =
            DependencyProperty.RegisterAttached("FadeInAnimation",
                                                typeof(Storyboard),
                                                typeof(ItemsSourceBehavior),
                                                new UIPropertyMetadata(null));
        public static void SetFadeInAnimation(DependencyObject element, Storyboard value)
        {
            element.SetValue(FadeInAnimationProperty, value);
        }
        public static Storyboard GetFadeInAnimation(DependencyObject element)
        {
            return (Storyboard)element.GetValue(FadeInAnimationProperty);
        }
    
        public static readonly DependencyProperty FadeOutAnimationProperty =
            DependencyProperty.RegisterAttached("FadeOutAnimation",
                                                typeof(Storyboard),
                                                typeof(ItemsSourceBehavior),
                                                new UIPropertyMetadata(null));
        public static void SetFadeOutAnimation(DependencyObject element, Storyboard value)
        {
            element.SetValue(FadeOutAnimationProperty, value);
        }
        public static Storyboard GetFadeOutAnimation(DependencyObject element)
        {
            return (Storyboard)element.GetValue(FadeOutAnimationProperty);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.