views:

474

answers:

2

I have a WPF ListBox control which displays items of an RSS feed. I occasionally check the source of the RSS feed for new items. Once I detect a new item I add it to the observable collection which immediately adds the new item to the ListBox display.

Is there a way to 'slide in' the new item from the top, pushing down the existing items? How would I achieve such an effect? Can it be done with a ListBox, or do I need to resort to my own container, such as a StackPanel and animate for example the Height of newly added controls programmatically?

+1  A: 

It can be done with a ListBox. Use the ItemContainerStyle to style the ListBoxItems that the binding creates for you: this style can include animations, e.g. by adding an EventTrigger for the Loaded event to the Style.Triggers, and transforms. For example, in your trigger action you could animate the Height so the item expands into place, or if the height is unknown you could have your style set a ScaleTransform and in your trigger action animate the ScaleY of that transform from 0 to 1.

itowlson
This seems doable, but I am running into problems with having Bindings in the StoryBoard (I am binding to the IsNew property so I am only animating new items, not all items when the ListBox is loaded initially).
Philipp Schmid
I'm not sure what the IsNew property is, but that would probably be part of a trigger rather than a binding in the storyboard; depends how you're doing it. You'd need to post some more details to diagnose the issue; but I'd suggest as a first step you put this aside and explore Josh's solution instead (it's far neater), and come back to mine only if his doesn't work for your scenario.
itowlson
+2  A: 

I just posted an answer to this question which is very similar to yours.

WPF how to animate a list of components

Josh Einstein
Nice one -- using a general-purpose animating panel, and then slotting that in via ItemsPanel, is neatly modular and beautifully idiomatic. Thank you!
itowlson