有没有比Grid或WrapPanel更好的方法来布局表单上的标签/值对?

jra*_*ser 5 c# wpf xaml

我正在寻找一种不那么繁琐的方法来布局键/值对(例如,标签上写着"名字",然后是带有名字的标签).如果它只是一个简单的分组,我会把它扔进网格并完成.然而,布局2或3对,接着是一些类型的分组容器,有4或5对,其次是不同的分组容器等.

做以下事情感觉非常麻烦:

<Grid>
     <Grid.ColumnDefinitions>
         <ColumnDefinition Width=".2*" />
         <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
         <RowDefinition />
         <RowDefinition />
      </Grid.RowDefinitions>

                <Label Grid.Column="0" Grid.Row="0" Content="Element ID:" HorizontalAlignment="Right" />
                <Label Grid.Column="1" Grid.Row="0" Content="{Binding Path=ElementId}" HorizontalAlignment="Left" FontWeight="Bold" />

                <Label Grid.Column="0" Grid.Row="1" Content="Element Description:" HorizontalAlignment="Right" />
                <Label Grid.Column="1" Grid.Row="1" Content="{Binding Path=ElementDescription}" HorizontalAlignment="Left" FontWeight="Bold" />                    
</Grid>
Run Code Online (Sandbox Code Playgroud)

只是渲染:

_______元素ID:ABC123

元素描述:漂亮!

当你有这样的几个部分时尤其如此.

UniformGrid不会让您修复一列,以便Label可以右对齐.我尝试了StackPanel和WrapPanel的组合,但最终会产生大量的开销和大量的边缘摆弄.

有一个更好的方法吗?

Wou*_*ter 3

您可以将Autogrid用于 xaml,如下所示:

<AutoGrid RowCount="2" RowHeight="35" Columns="100,auto">
  <Label />
  <TextBox />
  <Label />
  <TextBox />
</AutoGrid>
Run Code Online (Sandbox Code Playgroud)