R – How to access actual Height of Elements with Height=Auto

actualheightsilverlight

I have a situation like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="560"/>
        <ColumnDefinition Width="250"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" /> <!-- status infos & content start -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="Auto" /> <!-- status infos -->
        <RowDefinition Height="*"/>     <!-- content ends -->
    </Grid.RowDefinitions>

    <!-- image a list of custom controls directed to the first or second column on all rows here -->

    <SomeCustomControl Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="2" />
</Grid>

As you can see I have 2 Columns, the right one is more or less reserved for status information, the left for content. "SomeCustomControl" contains a control so wide it needs to be set to ColumnSpan="2". Notice there are still the status control in the right column. In SomeCustomControl I have something like this:

<Grid x:Name="LayoutRoot">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="250"/>
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        [...]
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*"  />   <!-- problem control here -->
        <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions>

    <!-- a list of Controls limited to the first column -->

    <ProblemControl Grid.Column="0" Grid.ColumnSpan="2" />
</Grid>

Now, the first Rows of SomeCustomControl contain controls limited to the first column, then there is a row that contains my ProblemControl. The Height of the status controls is not predetermined and depends on the shown status information. The controls in SomeCustomControl that are limited to the first column also have different heights, that are currently determined automatically through the content.

I now have the problem that ProblemControl overlaps with some of my status controls. I tried to calculate the height of my status controls and the limited controls in SomeCustomControl, but as all controls are sized dynamically I can't seem to get correct Heights. The Height of the RowDefinitions all contains Heights of type Auto and value 1, the Heights of the concrete Controls seems to be NaN.

Any ideas as to how I can calc the heights or prevent the overlapings in other ways.

Best Answer

I've encountered somewhat of the same problem, but came across the solution recently. The reason why you can't access the width and height properties of a control with width or height set to Auto is that the run time system is querying for the property values before they've been set. The properties ActualWidth and ActualHeight claim to get the rendered height of controls so in theory, you'd think you could simply wait until the SL application had finished loading and then perform your query, since the controls would be rendered by then and therefore, the ActualHeight/ActualWidth values should be set.

Sadly, this isn't the case either. There doesn't seem to be any guarantee when those values are set, so the workaround I used is to hook into the SizeChanged-event of the control whose values I want. SizeChanged is triggered whenever the width and height properties of a control are changed, so if I handle that event, I am guaranteed that the values are set to something other than NaN.

Do whatever logic you need to perform in a handler for that event, and you'll find the values are set.

Related Topic