4

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

グリッドだけが青い長方形で埋められますが、Canvasを同じように動作させ、青い長方形で埋めたいですか?

カスタムキャンバスを作成する準備ができていますが、その方法がわかりません。

助言がありますか?

4

2 に答える 2

11

次のようなことを試すことができます:

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

これにより、指定された要素の実際の寸法から寸法を取得するように長方形に指示されます。

Grid だけが塗りつぶされている理由は、それが子のサイズを伝える唯一のものだからです。StackPanel は、そのすべての子の合計サイズからサイズを取得し、Canvas は指定したサイズですが、子要素のサイズは変更しません。

于 2013-02-09T22:52:54.687 に答える