Silverlight: Working with ActualHeight & ActualWidth at Runtime

I came up against a problem, it seems like a pretty specific problem, but none the less the solution took me a while to figure out, so worth sharing!

My problem: I have a user control that contains some text among other things, but the text is of various length, the containing textblock therefore, like the user control has a dynamic height (set the Height property to System.Double.NaN).

There are n instances of the user control within a StackPanel, as a control is added to the StackPanel I want a StoryBoard which animates the Height and the Opacity.  The Height of course I do not know.

So I need to the set the Value of the Double Animation at runtime.

The XAML for the StoryBoard.

Code Snippet
  1. <Storyboard x:Name="sbExpandControl">
  2.     <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(FrameworkElement.Height)">
  3.         <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
  4.         <EasingDoubleKeyFrame KeyTime="00:00:02" Value="100" x:Name="expandToHeight"/>
  5.     </DoubleAnimationUsingKeyFrames>
  6.     <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="LayoutRoot" Storyboard.TargetProperty="(UIElement.Opacity)">
  7.         <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
  8.         <EasingDoubleKeyFrame KeyTime="00:00:02" Value="1"/>
  9.     </DoubleAnimationUsingKeyFrames>
  10. </Storyboard>

 

Firstly I need to new up the user control, set all the properties, add the control to the StackPanel, set the height of the DoubleAnimation and fire the StoryBoard.

Just simply going UserControl.ActualHeight will return 0 because the layout engine has not calculated the ActualHeight property. Which is the problem!!!

So we need to use the Dispatcher class (you can find out more on MSDN).

Code Snippet
  1. UserComment uc = new UserComment()
  2. {
  3.     Comments = v.Comments,
  4. };
  5.  
  6. stkComments.Children.Insert(0, uc);
  7. stkComments.Dispatcher.BeginInvoke(() =>
  8.     {
  9.         uc.ExpandControl();
  10.     });

In the User Control (UserComment)

Code Snippet
  1. public void ExpandControl()
  2. {
  3.     expandToHeight.Value = this.ActualHeight;
  4.     sbExpandControl.Begin();
  5. }

 

Hope this helps!

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Sunday, November 29, 2009 11:50 PM

Feedback

# re: Silverlight: Working with ActualHeight & ActualWidth at Runtime

Left by Carl at 6/9/2010 4:48 AM
Gravatar Awesome, saved me some time.

# re: Silverlight: Working with ActualHeight & ActualWidth at Runtime

Left by Entro at 12/27/2010 3:51 AM
Gravatar That was really a great tip.
Keep up the good work.

# re: Silverlight: Working with ActualHeight & ActualWidth at Runtime

Left by chris at 1/27/2012 10:44 AM
Gravatar Excellent work around, had my head banging against a wall for a few days on this one!

Your comment:





 
 

Copyright © Steve Clements

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski