如何阻止 Canvas 影响 UWP 中的网格大小
How to stop Canvas from affecting Grid size in UWP
在下面的代码中,用 Canvas 包围 Grid 限制了 Grid 的大小。然而,这种行为不会发生在 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>
正如@AVKNaidu 所建议的,"To fill it, you need to bind the width and height of Grid to Canvas Actual Width and Height."
工作代码:
<Canvas Background="AliceBlue" x:Name="TheCanvas">
<Grid BorderBrush="Green" BorderThickness="50"
Width="{Binding ActualWidth, ElementName=TheCanvas}"
Height="{Binding ActualHeight, ElementName=TheCanvas}">
...........
</Grid>
</Canvas>
在下面的代码中,用 Canvas 包围 Grid 限制了 Grid 的大小。然而,这种行为不会发生在 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>
正如@AVKNaidu 所建议的,"To fill it, you need to bind the width and height of Grid to Canvas Actual Width and Height."
工作代码:
<Canvas Background="AliceBlue" x:Name="TheCanvas">
<Grid BorderBrush="Green" BorderThickness="50"
Width="{Binding ActualWidth, ElementName=TheCanvas}"
Height="{Binding ActualHeight, ElementName=TheCanvas}">
...........
</Grid>
</Canvas>