问题描述
我有一组要显示在WPF窗口中的键/值对.我正在使用网格将它们布置成这样:
I have a set of Key/Value pairs I want to display on a WPF Window. I'm using a grid to lay them out like so:
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0">Code</Label>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Code}"/>
<Label Grid.Row="1" Grid.Column="0">Name</Label>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Name}"/>
</Grid>
但是,当我显示此文本框时,文本框被压缩,其顶部和底部边框触摸上方/下方的TextBox.在此布局的行中添加垂直空间的最佳方法是什么?
However when I display this, the TextBoxes are squashed up with their top and bottom borders touching the TextBox above/below. What is the best way to add vertical space to the rows in this layout?
推荐答案
最简单的方法是在各个控件上设置边距.在TextBoxes上设置它应该足够了,因为一旦它们间隔开了,Labels将垂直设置在每行的中心,并且仍然有足够的空间.
Easiest way is to set a margin on the individual controls. Setting it on the TextBoxes should be enough, because once they're spaced out the Labels will set vertically in the center of each row and have plenty of space anyway.
您可以使用一种样式设置一次:
You can set it once using a style:
<Grid Margin="4">
<Grid.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="0,0,0,4" />
</Style>
</Grid.Resources>
...
</Grid>
这将为网格内任何TextBox的底部添加4像素的边距.
This will add a 4-pixel margin to the bottom of any TextBox inside your grid.
这篇关于WPF网格中子控件之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!