WPF Grid 中子控件之间的间距

编程入门 行业动态 更新时间:2024-10-24 13:27:18
本文介绍了WPF Grid 中子控件之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一组要在 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 中子控件之间的间距

本文发布于:2023-11-12 01:05:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中子   间距   控件   WPF   Grid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!