Shawn de Wet
Shawn de Wet

Reputation: 5996

WPF Animation on listbox items

Any idea what approach I would follow to get the items of a databound listbox to "fly" into the their position in the listbox similarly to the effect you see when a deck of cards is dealt in those Windows Card Games? (I'm using WPF)

Upvotes: 5

Views: 3466

Answers (1)

Sheridan
Sheridan

Reputation: 69985

You would need to extend the Panel class and use it as the ItemsPanelTemplate of your ListBox.ItemsPanel property. It's really easy... there's just two methods to override; one to measure the items in the ListBox and one to arrange them. Here is a microsoft article on the subject.

Here is perhaps a more useful article [unfortunately, no longer available] that shows how to animate the items. For your effect, you would simply set the from value on your position animations to be the same location just off the viewable area for each of your items. For example, using a from position of 0, finalSize.Height would mean that each item would slide to its position from the bottom left corner of the ListBox. You could use your new animated Panel as follows:

<ListBox>
  <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
      <YourXmlNamespace:YourAnimatedPanel AnyCustomProperty="value" />
    </ItemsPanelTemplate>
  </ListBox.ItemsPanel>
</ListBox>

Upvotes: 3

Related Questions