问题描述
我有一个 WPF 表单,我想在上面放置一个标准表单.每个表单元素都有一个标签,然后是一个控件.很标准的东西.
I have a WPF form, I want to lay out a standard form onto it. Each form element will have a label, and then a control. Pretty standard stuff.
如果我使用换行面板,可能会导致标签和控件分离,但我希望它们保持在一起.是否有一些 WPF 等效于 <nobr/>
?
If I use a wrap panel, it can cause the label and the control to be separated, but I want them to stay together. Is there some WPF equivalent of <nobr/>
?
Grid 可以工作,并且允许跨列等,但是我真的很讨厌你在每个控件上指定列和行.这使得重新排序或将内容插入列表非常不方便.
Grid works, and allows for column spanning etc, however I really really hate that you specify the column and row on each control. This makes it extremely inconvenient to reorder or insert things into the list.
有没有办法让网格使用更多 HTML 样式的列/行,其中项目是它们所在行的子项,以便我可以轻松地重新排序?
Is there a way to get the grid to use more HTML style column/rows where the items are a child of the row they are in, so that I can re-order easily?
是否有其他控件可以让我轻松布局表单?
Is there some other control that will let me layout a form easily?
推荐答案
记住你可以嵌套面板:
<WrapPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal">
<Label>Some field</Label>
<TextBox>Some value</TextBox>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label>Another field</Label>
<TextBox>Another value</TextBox>
</StackPanel>
...
</WrapPanel>
此外,对于列式布局,Grid 的共享大小范围可以协调使用它的任意数量的网格:
Also, for columnar layouts, the shared size scope of the Grid can coordinate any number of grids that use it:
<StackPanel Orientation="Vertical" Grid.IsSharedSizeScope="True">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Label"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0">Some field</Label>
<TextBox Grid.Column="1">Some value</TextBox>
</Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Label"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0">Another field</Label>
<TextBox Grid.Column="1">Another value</TextBox>
</Grid>
</StackPanel>
我有点讨厌 XAML 的冗长,尤其是您必须重复列定义.虽然如果你正确地构建你的类并使用模板,它就不会那么糟糕了.请注意,您不会在此方案中的任何位置跟踪行号,因此重新排序字段很简单.
I kind of hate how verbose the XAML for this is, especially that you have to repeat the column definitions. Though if you structure your classes properly and use templates it's not so terrible. And notice that you aren't keep track of row numbers anywhere in this scheme, so reordering fields is simple.
这篇关于如何使用网格或其他控件在 WPF 中布局表单以实现可维护性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!