Tech Off Post

Single Post Permalink

View Thread: Silverlight: animate size change
  • User profile image
    Sven Groot

    spivonious said:

    Can you do it in the code-behind? When the content changes, just start animating the Border to the new size of the content. At least that's how I'd approach it in WPF.

    That's what I ended up doing. I'm not sure if the Blend FluidLayout stuff would've worked for me, I couldn't get it to look quite right.


    The item in question I wanted to resize was actually a databound item in a ListBox. I'd set it up so that the DataTemplate used for the items changes when an item is selected to show more information inside. In order to do this, I was handling the ListBox.SelectionChanged event to change the ContentTemplate of the containing ListBoxItem whenever they got selected.


    To solve the animation problem, I record the ListBoxItem's current ActualHeight, then set the new ContentTemplate, then call UpdateLayout which updates the DesiredSize property. I then create a Storyboard with a DoubleAnimation to animate the height from the old height to the new DesiredSize.Height. This works a charm. Smiley


    When the items get unselected, I simply change the Height property back to Double.NaN so they autosize again. Though I suppose I could animate that as well but currently I don't. Smiley