如何在WPF中使用网格或其他控件来布局表单以实现可维护性 [英] How do I layout a form in WPF using grid or other controls for maintainability

查看:181
本文介绍了如何在WPF中使用网格或其他控件来布局表单以实现可维护性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF表单,我想在它上面放置一个标准表单。每个表单元素都会有一个标签,然后是一个控件。相当标准的东西。



如果我使用包装面板,它可以使标签和控件分离,但我希望它们保持在一起。是否有一些WPF相当于< nobr />



网格工作,并允许列跨越等,但是我真的很讨厌你在每个控件上指定列和行。这使得重新排列或插入列表中的东西非常不方便。

有没有办法让网格使用更多HTML样式的列/行,其中的项目是它们所在行的子项,轻松订购?



是否有其他控件可以让我轻松布局表单?

解决方案

有没有一些WPF相当于nobr?


请记住,您可以嵌套panel:

 < WrapPanel Orientation =Horizo​​ntal> 
< StackPanel Orientation =Horizo​​ntal>
<标签>某些字段< /标签>
< TextBox>一些值< / TextBox>
< / StackPanel>
< StackPanel Orientation =Horizo​​ntal>
<标签>另一个栏位< / Label>
< TextBox>另一个值< / TextBox>
< / StackPanel>
...
< / WrapPanel>

另外,对于柱状布局,网格的共享尺寸范围可以协调使用任意数量的网格它:

 < StackPanel Orientation =VerticalGrid.IsSharedSizeScope =True> 
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =AutoSharedSizeGroup =Label/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< Label Grid.Column =0>一些字段< / Label>
< TextBox Grid.Column =1>一些值< / TextBox>
< / Grid>
<网格>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =AutoSharedSizeGroup =Label/>
< ColumnDefinition Width =*/>
< /Grid.ColumnDefinitions>
< Label Grid.Column =0>另一个字段< / Label>
< TextBox Grid.Column =1>另一个值< / TextBox>
< / Grid>
< / StackPanel>

有点讨厌这个XAML有多冗长,特别是你必须重复列定义。虽然如果你正确地构建你的类,并使用模板,它并不那么糟糕。并且请注意,您不会跟踪此计划中任何位置的行号,因此重新排序字段很简单。


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.

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 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.

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?

解决方案

is there some WPF equivalent of nobr?

Remember that you can nest panels:

<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>

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>

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中使用网格或其他控件来布局表单以实现可维护性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆