2013-03-03 141 views
0

我想要Grid和两列,其中一列中我有Label在另一个TextBox中显示一个值。但不幸的是我的标签和文本框显示在另一个上。我做错了什么?显示信息是否合适和优雅?也许你可以建议任何其他更好的方式来做到这一点?谢谢!WPF网格格式问题。

<StackPanel Grid.Row="0" Grid.Column="2" VerticalAlignment="Stretch">     
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <telerik:Label Grid.Column="0" Content="Name" Width="203" FontSize="16"/> 
     <telerik:Label Grid.Column="0" Content="Title" Width="203" FontSize="16"/> 
     <telerik:Label Grid.Column="0" Content="Phone" Width="203" FontSize="16"/> 
     <telerik:Label Grid.Column="0" Content="Email" Width="203" FontSize="16"/> 
     <telerik:Label Grid.Column="0" Content="Departament" Width="203" FontSize="16"/> 

     <TextBlock Grid.Column="1" x:Name="txtFullName" TextWrapping="Wrap" Height="26"/> 
     <TextBlock Grid.Column="1" x:Name ="txtTitle" TextWrapping="Wrap" Height="26"/> 
     <TextBlock Grid.Column="1" x:Name ="txtPhone" TextWrapping="Wrap" Height="26"/> 
     <TextBlock Grid.Column="1" x:Name ="txtEmail" TextWrapping="Wrap" Height="26"/> 
     <TextBlock Grid.Column="1" x:Name ="txtDepartment" TextWrapping="Wrap" Height="26"/> 

    </Grid>    
</StackPanel> 

回答

1

您需要将行添加到您的Grid。目前,只有1行(因为您没有RowDefinitions),所以每列中的所有项目将在单行中堆叠在另一列之上。

网格行的添加方式与您添加列的方式大致相同,然后以相同的方式告诉控件它们属于哪个行。你可以像添加行如下:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> <!-- * is the default width, so not required --> 
     <ColumnDefinition/>   <!-- will be the same as above due to default of * --> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> <!-- the * height is implied if not present --> 
     <RowDefinition Height="Auto"/> <!-- this will take up only as much space as necessary --> 
    </Grid.RowDefinitions> 

    <TextBox Grid.Column="0" Grid.Row="0" Text="Label 1"/> 
    <TextBox Grid.Column="0" Grid.Row="1" Text="Label 2"/> 
    <!-- etc... --> 
</Grid> 

与*一个高度的行会拉伸以填充可用空间(带*高度的所有行之间划分)和一排的“自动”将收缩高度所以它只占用必要的空间来显示其内容。

+0

谢谢!正是我需要的! – 2013-03-03 21:27:15

+0

很高兴我能帮到你。 – 2013-03-03 21:32:36