C# WP8在运行时获取网格(内容面板)高度
我有一个基本问题。我的MainPage.xaml代码如下所示C# WP8在运行时获取网格(内容面板)高度,c#,wpf,visual-studio-2012,windows-8,windows-phone-8,C#,Wpf,Visual Studio 2012,Windows 8,Windows Phone 8,我有一个基本问题。我的MainPage.xaml代码如下所示 <!--LayoutRoot is the root grid where all page content is placed--> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/>
<!--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>
我的消息框返回NaN
值,但我希望得到一个特定的值,而不是不确定的值
谢谢。您应该在加载的事件中检查内容面板的实际高度。在构造函数中为NaN,因为实际值尚未确定…根据建议
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DisplayMessage);
}
void DisplayMessage(object sender, RoutedEventArgs e)
{
MessageBox.Show(ContentPanel.ActualHeight.ToString());
}
这将返回我的内容面板的高度
谢谢。使用void main window_Loaded(object sender,RoutedEventArgs e){MessageBox.Show(ContentPanel.ActualHeight.ToString());}非常正确,但是-ActualHeight在初始化Component()后在构造函数中将为0。高度为NaN(不是数字),因为栅格高度设置为自动。但您是对的,检查加载事件中的实际高度可能是OP想要的。
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(DisplayMessage);
}
void DisplayMessage(object sender, RoutedEventArgs e)
{
MessageBox.Show(ContentPanel.ActualHeight.ToString());
}