Xander
Xander

Reputation: 932

WP8 get Grid (content panel) height in run time

I have a basic question. I have my MainPage.xaml code like this,

<!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">

            <TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" 
                       Margin="9,-7,0,0"  Style="{StaticResource PhoneTextTitle1Style}" Foreground="Black" />
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" >

        </Grid>
    </Grid>

In my MainPage.xaml.cs file,

public MainPage()
        {
            InitializeComponent();

            MessageBox.Show(ContentPanel.Height.ToString());
        }

My message box returns NaN value but I would like to get a specific value rather than indefinite value.

Thanks.

Upvotes: 1

Views: 517

Answers (2)

Xander
Xander

Reputation: 932

Based on the suggestions,

public MainPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(DisplayMessage);
        }

void DisplayMessage(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(ContentPanel.ActualHeight.ToString());
        }

This returns me height of my content panel.

Thanks.

Upvotes: 0

Robert Pabst
Robert Pabst

Reputation: 31

you should check the actual height of your content panel in the Loaded event. In the constructor it will be NaN because the actual values are not yet determined...

Upvotes: 2

Related Questions