c# - 如何在 XAML 中的网格单元格上拉伸(stretch)矩形

标签 c# windows-8 winrt-xaml

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

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

ColumnSpanStretch也不工作。 这是代码:

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

最佳答案

你试过吗:

<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}}}"/>

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

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

编辑:我忘了。我对 Rectangle 本身并没有太多的了解,但这也可能有效:

<Rectangle x:Name="underlineRect" Stretch="UniformToFill" Height="2" Fill="White"
           Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1"
           HorizontalAlignment="Stretch"/>

关于c# - 如何在 XAML 中的网格单元格上拉伸(stretch)矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17309045/

相关文章:

c# - WinRTXamlToolkit.Controls.DataVisualization.Charting 不适用于 .appx bundle

c# - 使用 LINQ 过滤必要的数据

c# - 从 excel 工作表中获取列名称

c# - 带有可见可点击控件的透明表单

javascript - 将动态数据从 API 绑定(bind)到 FlipView 控件

xaml - 访问文本 block 的背景色

c# - 如何在 asp.net 中使用 autopostback=false 检查另一个单选按钮时取消选中一个单选按钮

c# - 我可以防止类被非抽象对象继承吗?

c# - 如何查询 Windows 8 的人脉应用程序?

c# - 将 `Page` 替换为 `WinRTXamlToolkit.Controls.AlternativePage`