【问题标题】:How to define a grid layout如何定义网格布局
【发布时间】:2013-09-20 06:51:02
【问题描述】:

我仍然不明白如何定义灵活的网格布局... 我想要一个有 n 行(我不知道有多少)和 m 列(我不知道有多少)的网格。我希望单元格具有特定的高度和宽度(50 和 50)....

这就是我可以设置行和列定义的方式:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="350" />
    <ColumnDefinition Width="130" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="2*" />
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

但是这种方式我只是设置了两列三行...有没有办法为每列和网格设置一个列定义和一个行定义?

感谢

【问题讨论】:

    标签: layout windows-phone-8 grid


    【解决方案1】:

    你就是这样做的

    public MainPage()
        {
            InitializeComponent();
            var def = new ColumnDefinition();
            def.Width = new GridLength(100);
            gdTest.ColumnDefinitions.Add(def);
            StackPanel sp = new StackPanel();
            sp.Background = new SolidColorBrush( Colors.Brown);
            sp.SetValue(Grid.ColumnProperty, 1);
            gdTest.Children.Add(sp);
    
        }
    

    Xaml

    <Grid x:Name="gdTest" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Margin="5,0,5,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
    
        <StackPanel Grid.Column="0" x:Name="TestStack" Background="Red"/>
    
    
    </Grid>
    

    添加代码并测试:)

    【讨论】:

      【解决方案2】:

      继续第一个问题,这就是你如何开始 C# :-

               Grid Layout = new Grid();
      
               foreach (your condition)
               { 
               //
                   Layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
                   Layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(50)});
                   Layout.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50) });
               }
      

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 1970-01-01
        • 1970-01-01
        • 2018-09-01
        • 2014-11-02
        • 1970-01-01
        • 1970-01-01
        • 2012-03-04
        • 1970-01-01
        相关资源
        最近更新 更多