3

キャンバスをその親に「ドッキング」するにはどうすればよいですか?

内部にキャンバスを含む UserControl があります。

<UserControl x:Class="MyUC"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d"              
         d:DesignHeight="300" d:DesignWidth="300">
    <MyCanvas x:Name="myCanvas" 
        Height="???" 
        Width="???{Binding RelativeSource={RelativeSource TemplatedParent}}" >
    </MyCanvas>
</UserControl>

内部でこのカスタム キャンバスのプロパティを使用Widthします。Heightまた、そのプロパティが常に親コンテナに「バインド」されている必要があります。

4

3 に答える 3

5

これを試して

Width="{Binding RelativeSource={RelativeSource FindAncestor, 
                                               AncestorType=UserControl, 
                                               AncestorLevel=1},
                Path=ActualWidth}"

身長も同じ

于 2010-10-27T10:09:21.720 に答える
2

WidthとのHeightプロパティを設定しないと、Canvasで使用可能なすべてのスペースが占有されますUserControl。簡単な例を次に示します。

[メインウィンドウ.xaml]

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Width="500" Height="500"
        x:Class="WpfApplication1.MainWindow">
    <Grid Background="Blue">
        <local:UserControl1 />
    </Grid>
</Window>

[UserControl1.xaml]

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Background="Green">
<Canvas Background="Red" />

このアプリを実行すると、背景色が赤であることがわかります。これは、が(およびその親)Canvasによって使用可能になったすべてのスペースを占めることを意味します。ウィンドウのサイズを変更することもできます。UserControlGridCanvas

于 2010-10-27T10:35:15.553 に答える