【问题标题】:WP8 get Grid (content panel) height in run timeWP8 在运行时获取网格(内容面板)高度
【发布时间】:2014-01-29 13:31:35
【问题描述】:

我有一个基本问题。我有这样的 MainPage.xaml 代码,

<!--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>

在我的 MainPage.xaml.cs 文件中,

public MainPage()
        {
            InitializeComponent();

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

我的消息框返回NaN 值,但我想获得一个特定的值而不是不确定的值。

谢谢。

【问题讨论】:

  • 使用 void MainWindow_Loaded(object sender, RoutedEventArgs e) { MessageBox.Show(ContentPanel.ActualHeight.ToString()); }

标签: c# wpf visual-studio-2012 windows-8 windows-phone-8


【解决方案1】:

您应该在 Loaded 事件中检查内容面板的实际高度。在构造函数中,它将是 NaN,因为尚未确定实际值...

【讨论】:

  • 非常正确,但是在 InitializeComponent() 之后,构造函数中的 ActualHeight 将为 0。高度为 NaN(不是数字),因为网格高度设置为自动。但是你是对的,在 Loaded 事件中检查 ActualHeight 是 OP 可能正在寻找的。​​span>
【解决方案2】:

根据建议,

public MainPage()
        {
            InitializeComponent();

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

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

这会返回我的内容面板的高度。

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-17
    • 2016-10-28
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多