C# 在WPF中调整网格内的画布

C# 在WPF中调整网格内的画布,c#,wpf,canvas,C#,Wpf,Canvas,在WPF中调整网格内的画布时遇到问题。我希望它在网格的右侧和顶部有10px的边距。以下代码中我做错了什么 <Window x:Class="Layout2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainW

在WPF中调整
网格内的
画布
时遇到问题。我希望它在
网格的
右侧和
顶部有10px的边距。以下代码中我做错了什么

<Window x:Class="Layout2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="DrawingArea" Background="Black">
        <Canvas x:Name="InformationLayer" 
                    Background="White" 
                    HorizontalAlignment="Right"
                    VerticalAlignment="Top"
                    Right="10"
                    Top="10"
                    Width="200" Height="30" >
        </Canvas>
    </Grid>
</Window>


Right
Top
画布
类的附加属性,用于将元素定位在父
画布
对象中。我不相信它们在
画布
标记本身中使用时具有语义意义(当然,除非您嵌套在画布中)

相反,请使用边距属性:

   <Canvas x:Name="InformationLayer" 
                Background="White" 
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Margin="0,10,10,0"
                Width="200" Height="30" >
    </Canvas>


页边距的格式为“左、上、右、下”,以防您需要修改

非常感谢你,布拉德利,我没想到会有这么简单的解决办法。