【问题标题】:Make a grid into a grid XAML将网格变成网格 XAML
【发布时间】:2014-03-04 21:43:46
【问题描述】:

我想创建一个网格成一个网格。我尝试了以下代码,但这不起作用,有人知道为什么吗?你有解决办法吗?

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ShowGridLines="true">
  <Grid.Background>
     <LinearGradientBrush>
        <GradientStop Color="#EEE8AA" />
        <GradientStop Color="#2F4F4F" Offset="1" />
     </LinearGradientBrush>
  </Grid.Background>
  <Grid.RowDefinitions>
     <RowDefinition Height="10*" />
     <RowDefinition Height="90*" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
     <ColumnDefinition Width="10*" />
     <ColumnDefinition Width="80*" />
     <ColumnDefinition Width="10*" />
  </Grid.ColumnDefinitions>
  <Grid Grid.Row="0" Grid.Column="1">
     <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
     </Grid.ColumnDefinitions>
     <Grid.RowDefinitions>
        <RowDefinition Height="100*" />
     </Grid.RowDefinitions>
  </Grid>
</Grid>

【问题讨论】:

  • 代码没有错,尝试将一些内容添加到网格中,例如 TextBlock 等,或者只是为了确保其正常工作,使用背景颜色设置网格的宽度和高度。
  • “不起作用”是什么意思?

标签: xaml grid


【解决方案1】:

代码是正确的。您不会看到任何内容,因为第二个网格仅填充父网格中的特定网格单元并且没有内容。尝试添加一些文本框或颜色,您会发现效果很好。

请参阅下面的示例。您将在第二个网格中正确显示 3 个文本框。

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ShowGridLines="true">
    <Grid.Background>
      <LinearGradientBrush>
        <GradientStop Color="#EEE8AA" />
        <GradientStop Color="#2F4F4F" Offset="1" />
      </LinearGradientBrush>
    </Grid.Background>
    <Grid.RowDefinitions>
      <RowDefinition Height="10*" />
      <RowDefinition Height="90*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="10*" />
      <ColumnDefinition Width="80*" />
      <ColumnDefinition Width="10*" />
    </Grid.ColumnDefinitions>
    <Grid Grid.Row="0" Grid.Column="1">
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition Height="100*" />
      </Grid.RowDefinitions>
      <TextBlock Grid.Column="0" Grid.Row="0">TextBox1</TextBlock>
      <TextBlock Grid.Column="1" Grid.Row="0">TextBox2</TextBlock>
      <TextBlock Grid.Column="2" Grid.Row="0">TextBox3</TextBlock>
    </Grid>
  </Grid>

【讨论】:

    【解决方案2】:

    如果您想直观地查看网格而不向其添加内容,至少尝试将其设置为 Height/Width 为固定值。我将您的代码复制粘贴到StackPanel 中,将外部GridHeight 设置为100:

    <StackPanel Background="Gray">
        <Grid ShowGridLines="true" Height="100" >
            .......
        </Grid>
    </StackPanel>
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-02
      相关资源
      最近更新 更多