WPF网格中子控件之间的间距

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

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

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

发布评论

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

>www.elefans.com

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