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.
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
ObservableCollectionnormally (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 theCollectionChangedevent.Here is an implementation I made of this, using an attached behavior. It can be used for
ItemsControl,ListBox,DataGridor anything else that derives fromItemsControl.Instead of Binding
ItemsSource, bind the attached propertyItemsSourceBehavior.ItemsSource. It will create a mirrorObservableCollectionusing Reflection, use the mirror asItemsSourceinstead and handle theFadeIn/FadeOutanimations.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
ItemsSourceBehavior