如何在XAML中拉伸网格单元格上的矩形

ash*_*khe 7 c# windows-8 winrt-xaml

我需要在网格的第二行添加一个矩形.我需要矩形的宽度与网格的宽度相同.

但问题是,网格的宽度是在运行时决定的.如果我尝试访问WidthActualWidth在后面的代码,我得到NaN0.0分别.

ColumnSpan并且Stretch也没有工作.这是代码:

<Grid x:Name="downloadPdfGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="20"/>
        <RowDefinition Height ="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Button x:Name="btn" Content="{Binding Button}" Visibility="Collapsed" Click="OnButtonClick" Grid.Row="0"/>
    <Rectangle x:Name="underlineRect" Stretch="UniformToFill" Height="2" Fill="White" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)

Sim*_*ger 15

你有没有尝试过:

<Rectangle x:Name="underlineRect" Stretch="UniformToFill" Height="2" Fill="White"
           Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"
           Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}}"/>
Run Code Online (Sandbox Code Playgroud)

或者,如果您有网格名称:

<Rectangle x:Name="underlineRect" Stretch="UniformToFill" Height="2" Fill="White"
           Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"
           Width="{Binding ActualWidth, ElementName=downloadPdfGrid}"/>
Run Code Online (Sandbox Code Playgroud)

编辑:我忘了.我本身并没有使用Rectangle,但这也可能有用:

<Rectangle x:Name="underlineRect" Stretch="UniformToFill" Height="2" Fill="White"
           Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"
           HorizontalAlignment="Stretch"/>
Run Code Online (Sandbox Code Playgroud)