XAML如何让Canvas的Child填充整个Canvas?

Joh*_*ohn 3 windows-8 winrt-xaml

如果我有这个XAML:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<StackPanel Orientation="Horizontal">
    <Grid Background="Gray" Margin="5"
        Height="200" Width="200">
        <Rectangle Fill="Blue" />
    </Grid>
    <Canvas Background="Gray" Margin="5"
        Height="200" Width="200">
        <Rectangle Fill="Blue" />
    </Canvas>
    <StackPanel Background="Gray" Margin="5"
        Height="200" Width="200">
        <Rectangle Fill="Blue" />
    </StackPanel>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

只有网格被蓝色矩形填充,但我希望Canvas以相同的方式操作并填充蓝色矩形?

我想制作一个自定义画布,但不知道如何去做.

有什么建议?

Chr*_*isF 10

你可以尝试这样的事情:

<Canvas x:Name="myCanvas" Background="Gray" Margin="5"
    Height="200" Width="200">
    <Rectangle Fill="Blue"
               Height="{Binding ElementName=myCanvas, Path=ActualHeight}"
               Width="{Binding ElementName=myCanvas, Path=ActualWidth}" />
</Canvas>
Run Code Online (Sandbox Code Playgroud)

这将告诉矩形从命名元素的实际尺寸中获取它的尺寸.

仅填充网格的原因是因为它是唯一一个告诉它的孩子大小的原因.StackPanel从它所有孩子的组合大小中获取它的大小,而Canvas是你告诉它的大小,但不会调整任何子元素的大小.