如何在WPF中的网格上放置边框? [英] How do i put a border on my grid in WPF?

查看:63
本文介绍了如何在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天全站免登陆