在WPF中调整网格内的画布

3

在WPF中,我调整Grid里的Canvas时遇到了麻烦。我希望它距离GridRightTop两侧都有10像素的边距。请问以下代码有何问题?

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

enter image description here

1个回答

5
RightTopCanvas类的附加属性,用于在父Canvas对象中定位元素。我认为它们在Canvas标签本身中使用时没有语义意义(除非您嵌套在画布中)。
相反,请使用margin属性:
   <Canvas x:Name="InformationLayer" 
                Background="White" 
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Margin="0,10,10,0"
                Width="200" Height="30" >
    </Canvas>

边距的格式是“左,上,右,下”,以便您需要进行修改!

非常感谢你,Bradley。我没想到会有这么简单的解决方案。 - Vahid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接