如何在WPF中的网格控件上放置边框? [英] How could I put a border on my grid control in WPF?

查看:87
本文介绍了如何在WPF中的网格控件上放置边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#/WPF中的网格上放置边框?

How do I put a border on my grid in C#/WPF?

这就是我想要的样子,但是在整个内容周围放置了边框,而不是在应用程序中放置的网格控件.

This is what I would like it to be, but puts a border around the whole thing instead of the grid control I put in my application.

<Grid>
    <Border BorderBrush="Black" BorderThickness="2">
        <Grid Height="166" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top" Width="479" Background="#FFF2F2F2" />
    </Border>
... and so on ...

推荐答案

如果只需要一个外部边框,最简单的方法是将其放在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>

看到边框完全填充控件的原因是,默认情况下,将Horizo​​ntalAlignment和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...)

这篇关于如何在WPF中的网格控件上放置边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆