更新时间:2021-10-15 03:28:29
如果只需要一个外部边框,最简单的方法是将其放在Border控件中:
If you just want an outer border, the easiest way is to put it in a Border control:
<Border BorderBrush="Black" BorderThickness="2">
<Grid>
<!-- Grid contents here -->
</Grid>
</Border>
看到边框完全填充控件的原因是,默认情况下,将HorizontalAlignment和VerticalAlignment设置为Stretch.请尝试以下操作:
The reason you're seeing the border completely fill your control is that, by default, it's HorizontalAlignment and VerticalAlignment are set to Stretch. Try the following:
<Grid>
<Border HorizontalAlignment="Left" VerticalAlignment="Top" BorderBrush="Black" BorderThickness="2">
<Grid Height="166" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479" Background="#FFF2F2F2" />
</Border>
</Grid>
这应该可以让您得到满意的结果(尽管您可能希望在所有4个方面都留出边距,而不仅仅是2个...)
This should get you what you're after (though you may want to put a margin on all 4 sides, not just 2...)