ActualWidth 和 ActualWidth 为 0

ActualWidth and ActualWidth is 0

首先是的,我已经阅读了所有建议 "Questions that may already have your answer",所以我相信这不是重复的。

我在页面上有这个网格:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="70"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="Please sign below." Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Image x:Name="SignImage" />
    </Grid>
    <Grid Background="Transparent" Grid.Row="2" Margin="12,0,10,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="10"/>
        </Grid.ColumnDefinitions>

        <Button x:Name="ClearButton" Content="Clear" Grid.Column="1"/>
        <Button x:Name="OkButton" Content="Ok" Grid.Column="2"/>
    </Grid>
</Grid>

然后这个代码隐藏:

public MainPage()
{
    InitializeComponent();
    this.Loaded += MainPage_Loaded;
    // Sample code to localize the ApplicationBar
    //BuildLocalizedApplicationBar();
}

.....

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
  MessageBox.Show($"SignImage.ActualWidth: {SignImage.ActualWidth}, SignImage.ActualWidth: {SignImage.ActualWidth} ");
}

问题是我的图像大小为 0(零)。

如何让我的图片填满我放置它的单元格?

首先*和Auto有区别。请检查主网格的大小。 “*”表示拉伸以填充可用 space,并且是一个分数,因此如果一行是 * 另一行是 2* 那么第二行将是第一行大小的两倍。

Auto 表示只需要 whatever/or space 它需要。

如果您将整个Grid 的尺寸设置为较小的高度,则会出现问题。为 Main Grid 组件设置 MinHeight。

其次,您没有为 SignImage 设置图像大小,这意味着您显然会将 ActualWidth 和 ActualHeight 设置为零。图片标签为空,其中未设置任何来源。

<Image x:Name="SignImage" Source="C:\Users\Administrator\Pictures\user-thumbnail.png"/>

设置图像源后检查是否获得 ActualWidth 和 ActualHeight

看看这张图片我已经写了你的代码。您可以看到,对于网格 0 和 2,为网格 1 设置了高度,因为您没有为它编写 * 设置高度,因此有必要将高度设置为父级。

现在看这张图,我已经将父元素的高度设置为 200,因此您可以看到 Gird 1 是可见的,高度不是 0。

所以需要设置高度或者设置图片来源让高度不为0