-1

This is my c# code:

double height = grid.ActualHeight;
double newHeight = height - rectangle.ActualHeight;
topRectangle.Height = newHeight;
label.Content = "height: " + height + "\nNew Height: " + newHeight;

and the xaml file is this:

<Grid x:Name="grid" Height="Auto" Width="Auto">
    <Rectangle x:Name="rectangle" Fill="#FF181818" HorizontalAlignment="Left" Height="222" Margin="0,0,-0.333,0" Stroke="#FF181818" VerticalAlignment="Bottom" Width="{Binding ActualWidth, ElementName=grid}"/>
    <Rectangle x:Name="topRectangle" Fill="#FF181818" HorizontalAlignment="Left" Height="285" Margin="0,0,0,221.667" Stroke="#FF181818" VerticalAlignment="Bottom" Width="190"/>
    <Label x:Name="label"  Content="Label" HorizontalAlignment="Left" Margin="306,99,0,0" VerticalAlignment="Top" Foreground="White"/>
</Grid>

The label's text at the moment of running is this:

height: 0

New Height: 0

but for more I try this always keeps giving out 0 and the topRectangle is not seen because of height 0. I tried setting topRectangle.Height instead of ActualHeight and it was:

height: NaN

Actual Height: NaN

if it is not clear in code, the height of grid and rectangle is NOT 0

Any answers?

Nekidev
  • 69
  • 1
  • 1
  • 8

1 Answers1

1

Set HorizontalAlignment to Stretch instead of binding its width to its parent container which needs to measure its content due to Auto.

<Grid x:Name="grid" Height="Auto" Width="Auto">
    <Rectangle x:Name="rectangle" Fill="#FF181818" HorizontalAlignment="Stretch" Height="222" Margin="0,0,-0.333,0" Stroke="#FF181818" VerticalAlignment="Bottom"/>
    <Rectangle x:Name="topRectangle" Fill="#FF181818" HorizontalAlignment="Left" Height="285" Margin="0,0,0,221.667" Stroke="#FF181818" VerticalAlignment="Bottom" Width="190"/>
    <Label x:Name="label"  Content="Label" HorizontalAlignment="Left" Margin="306,99,0,0" VerticalAlignment="Top" Foreground="White"/>
</Grid>
TimTIM Wong
  • 788
  • 5
  • 16