如何阻止Canvas影响UWP中的网格大小

时间:2017-05-19 16:44:13

标签: uwp uwp-xaml

在下面的代码中,使用Canvas围绕网格会限制网格大小。但是,这种行为不会发生在WPF中。

    <Canvas Background="AliceBlue">
    <Grid BorderBrush="Green" BorderThickness="50" Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="BorderBrush" Value="Yellow" />
                <Setter Property="BorderThickness" Value="5" />
                <Setter Property="HorizontalAlignment" Value="Stretch" />
                <Setter Property="VerticalAlignment" Value="Stretch" />
            </Style>
        </Grid.Resources>
        <!--<Button  Content="Button"  />-->
    </Grid>
</Canvas>

enter image description here

1 个答案:

答案 0 :(得分:1)

正如@AVKNaidu建议的那样,“要填充它,您需要将网格的宽度和高度绑定到画布实际宽度和高度。”

工作代码

<Canvas Background="AliceBlue" x:Name="TheCanvas">
    <Grid BorderBrush="Green" BorderThickness="50" 
          Width="{Binding ActualWidth, ElementName=TheCanvas}" 
          Height="{Binding ActualHeight, ElementName=TheCanvas}">
         ...........
    </Grid>
</Canvas>