【问题标题】:Grid inside Grid in XAMLXAML 中网格内的网格
【发布时间】:2010-07-06 10:41:37
【问题描述】:

我想在 parentGrid 的第二列中有一个 childGrid(在 childGrid 中我想有两列:第一列用于标签,第二列用于文本框)

我怎么能做这样的事情?我尝试了以下代码:

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

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    根据您的代码,稍作修正:

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

    注意ColumnDefinition 没有高度 - 它们有宽度。您还需要分别定义 ColumnDefinitions 和 RowDefinitions - 您将它们混合在外部网格中。我从外部网格中删除了 RowDefinitions,因为您似乎没有使用它们。您的内部网格有两列和四行。

    【讨论】:

    • 如何在内部网格中放置控件?
    • @Gener4tor 在控件上使用Grid.RowGrid.Column 附加属性。因为这是一条评论,所以我必须简短,但它看起来像这样:&lt;Grid&gt;&lt;Grid.ColumnDefinitions /&gt;&lt;Grid.RowDefinitions /&gt;&lt;TextBox Grid.Row="1" Grid.Column="2"&gt;some test&lt;/TextBox&gt;&lt;/Grid&gt; 当然,你会有一些行/列定义,为了简洁起见,我将它们留空。请记住,行/列计数是从零开始的。
    • 好的。知道了。 Grid.Row="1" 和 Grid.Column="2" 始终指代最内层的网格。
    • 代码如何知道您引用了哪个 Grid 控件的行和列?这似乎不是一个明确的答案。你能在每个 Grid 上的不同位置显示几个控件吗?
    • @omJohn8372 行/列位置在控件(或网格)是子级的网格的上下文中。所以是的,您可以在所示的两个网格中的每一个中拥有更多控件/网格以上。
    【解决方案2】:

    您可能会发现这很有用。尝试使用 Kaxaml 将其粘贴到页面中,并使用外部 Grid 中对象的各种参数。我发现使用 Kaxaml 进行原型设计和试验 XAML 布局必不可少。

    <Grid>  
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
    
      <!-- 
         When I'm composing grids in XAML, I group things together by type, not by where
         they live in the grid.  This turns out to make a lot of maintenance tasks
         easier.
    
         Also, since Grid.Row and Grid.Column default to 0, a lot of people (and tools)
         omit them if that's their value.  Not me.  It lets me quickly check to make
         sure that content is where I think it is, just by looking at how it's organized
         in the XAML.
      -->
    
      <TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Background="Lavender" Padding="10" HorizontalAlignment="Stretch">Here's the first row of the outer grid.</TextBlock>
      <TextBlock Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Background="Lavender" Padding="10" HorizontalAlignment="Stretch">Here's the third row of the outer grid.</TextBlock>
    
      <TextBlock Grid.Row="1" Grid.Column="0" Background="AliceBlue" Padding="10">Here's the first column of the second row.</TextBlock>
    
      <Grid Grid.Row="1" Grid.Column="1">
        <Grid.ColumnDefinitions>
          <!--
             This part's pretty important.  Setting up the SharedSizeGroups for these
             two columns keeps the labels and text boxes neatly arranged irrespective of
             their length.
          -->
          <ColumnDefinition SharedSizeGroup="Label"/>
          <ColumnDefinition SharedSizeGroup="TextBox"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition/>
          <RowDefinition/>
          <RowDefinition/>
        </Grid.RowDefinitions>
    
        <Label Grid.Row="0" Grid.Column="0">First label</Label>
        <Label Grid.Row="1" Grid.Column="0">Second label</Label>
        <Label Grid.Row="2" Grid.Column="0">Third label, containing unusually long content</Label>
    
        <TextBox Grid.Row="0" Grid.Column="1">First text box, containing unusually long content</TextBox>
        <TextBox Grid.Row="1" Grid.Column="1">Second text box</TextBox>
        <TextBox Grid.Row="2" Grid.Column="1">Third text box</TextBox>
    
      </Grid>
    
    </Grid>
    

    【讨论】:

      【解决方案3】:

      如何在子网格中放置控件可能会有点混乱。这是一个例子。

      我们有 3 * 3 的网格。然后将中心单元格进一步分为 3 行,每行都有一个按钮。

      <Grid>
          <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="*"/>
              <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Grid Grid.Row="1" Grid.Column="1">
              <Grid.RowDefinitions>
                  <RowDefinition Height="*"/>
                  <RowDefinition Height="*"/>
                  <RowDefinition Height="*"/>
              </Grid.RowDefinitions>
                  <Button Grid.Row="0" Content="Button1"/>
                  <Button Grid.Row="1" Content="Button2"/>
                  <Button Grid.Row="2" Content="Button3"/>
          </Grid>
      </Grid>
      

      结果:

      【讨论】:

        【解决方案4】:

        Phenevo,我今年做了很多 XAML UI 设计。试试这个,您可以轻松地将代码迁移到 Window 或 UserControl。我对网格和面板进行了颜色编码,以便您可以实时确认它们的布局——当您满意时,将背景参数吹走。

        <UserControl
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            x:Class="UatControlLibrary.sampleChilGrid"
            x:Name="UserControl"
            MinWidth="400"
            MinHeight="300"
            Width="auto"
            Height="auto">
            <Grid
                x:Name="LayoutRoot">
                <Grid
                    x:Name="parentGrid"
                    Width="auto"
                    Height="auto"
                    Background="Red">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition
                            Width="1*" />
                        <ColumnDefinition
                            Width="1*" />
                    </Grid.ColumnDefinitions>
                    <Grid
                        x:Name="chilGrid"
                        Width="auto"
                        Height="auto"
                        Background="Black"
                        Grid.Column="1"
                        Grid.Row="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition
                                Width="1*" />
                            <ColumnDefinition
                                Width="1*" />
                        </Grid.ColumnDefinitions>
                        <StackPanel
                            x:Name="stkpnlLabels"
                            Background="White"
                            Grid.Column="0"
                            Grid.Row="0" />
                        <StackPanel
                            x:Name="stkpnlTextboxes"
                            Background="Blue"
                            Grid.Column="1"
                            Grid.Row="0" />
                    </Grid>
                </Grid>
            </Grid>
        </UserControl>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-28
          相关资源
          最近更新 更多