无法在Grid中设置特定列

时间:2015-12-05 14:53:38

标签: c# wpf xaml

我创建了一个TabItem,其默认布局包含3列和1行。在第一列中,我插入了StackPanel并放入另一个有3列3行的网格中。现在我在第二个网格内部的问题是创建了一个StackPanel的GroupBox,我设置了Grid.Column="2"GroupBox内的控件没有&# #39;进入第二个Grid的第二列,我不知道我做错了什么。这是我的代码:

<TabItem Header="Confronto">
     <Grid>
           <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="*" />
                 <ColumnDefinition Width="*" />
                 <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                  <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <StackPanel Grid.Column="0" Grid.Row="0">
                 <Grid>
                    <Grid.ColumnDefinitions>
                         <ColumnDefinition Width="*" />
                         <ColumnDefinition Width="*" />
                         <ColumnDefinition Width="*" />
                     </Grid.ColumnDefinitions>
                     <Grid.RowDefinitions>
                          <RowDefinition Height="*"/>
                          <RowDefinition Height="*"/>
                          <RowDefinition Height="*"/>
                     </Grid.RowDefinitions>

                     <GroupBox Header="Informazioni Squadre" Height="Auto" Grid.ColumnSpan="3" Grid.RowSpan="3">
                            <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Grid.Column="2">
                                 <Label Content="Inter" FontWeight="Bold"></Label>
                                 <Canvas Height="75" Width="75" Background="Gray"></Canvas> 
                                 <Label Content="Italia" Margin="0,15,0,0"></Label>
                                 <Label Content="Serie A" Margin="0,10,0,0"></Label>
                                 <Label Content="97%" Margin="0,10,0,0"></Label>
                             </StackPanel>
                      </GroupBox>

                    </Grid>
              </StackPanel>
       </Grid>
 </TabItem>

1 个答案:

答案 0 :(得分:1)

正如评论中所述,您只能对Grid.Column的直接子女设置Grid.RowGrid。您的内心Grid只有一个直接子(GroupBox),而Grid.Column \ Grid.Row仅适用于该元素。

您可以撤消订单并将内部Grid置于GroupBox内。不是Grid.Column="2"的一方会将它放在内部Grid的第3列而不是第二列(它从0开始索引)

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

    <StackPanel Grid.Column="0" Grid.Row="0">
        <GroupBox Header="Informazioni Squadre" Height="Auto">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Grid.Column="2">
                    <Label Content="Inter" FontWeight="Bold"></Label>
                    <Canvas Height="75" Width="75" Background="Gray"></Canvas>
                    <Label Content="Italia" Margin="0,15,0,0"></Label>
                    <Label Content="Serie A" Margin="0,10,0,0"></Label>
                    <Label Content="97%" Margin="0,10,0,0"></Label>
                </StackPanel>
            </Grid>
        </GroupBox>
    </StackPanel>
</Grid>