我有一组要在 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>但是,当我显示它时,TextBoxes 被压扁,它们的顶部和底部边框接触到 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 上应该就足够了,因为一旦它们被隔开,标签将垂直设置在每行的中心并且无论如何都有足够的空间.
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 Grid 中子控件之间的间距
发布评论