【问题标题】:Create multiple rows and columns xaml创建多行多列 xaml
【发布时间】:2014-08-24 15:07:33
【问题描述】:

我需要创建很多行和列才能正确管理我的自定义控件。所以我的问题是是否有可能达到与下面显示的代码相同的结果?用更干净的方式,这感觉太不切实际了……

<Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>

【问题讨论】:

  • 不,这是唯一的方法。你能重新组织你的页面,也许使用一些用户控件?
  • 或者您可以对网格进行子类化并为您需要的行数和列数添加属性
  • 您可以改用&lt;ColumnDefinition /&gt;&lt;RowDefinition /&gt; 来缩短定义。您是否需要定义定义的宽度和高度,还是应该始终为默认值?

标签: c# wpf


【解决方案1】:

您可以在后面的代码中执行相同的操作,但通常您应该像以前那样在 XAML 中执行此操作。

        // Add 10 Rows
        for (int i = 0; i < 10; i++)
        {
            var height = GridLength.Auto;
            if (i == 0)
                height = new GridLength(1, GridUnitType.Star);
            layoutGrid.RowDefinitions.Add(new RowDefinition()
            {
                Height = height
            });    
        }

        // Add 7 Columns
        for (int i = 0; i < 7; i++)
        {
            var width = GridLength.Auto;
            if (i == 0)
                width = new GridLength(1, GridUnitType.Star);
            layoutGrid.ColumnDefinitions.Add(new ColumnDefinition()
            {
                Width = width
            });
        }

【讨论】:

    【解决方案2】:

    您可以查看AutoGrid 的语法如下:

    <AutoGrid RowCount="2" RowHeight="35" Columns="100,auto">
      <Label />
      <TextBox />
      <Label />
      <TextBox />
    </AutoGrid>
    

    【讨论】:

      【解决方案3】:

      Wouter 对 AutoGrid 的回答的另一种选择是 ApexGrid。该代码看起来与 AutoGrid 非常相似。

      <apex:ApexGrid Rows="Auto,*" Columns="100,Auto">
         <Label Grid.Row="0" Grid.Column="0" />
         <TextBox Grid.Row="0" Grid.Column="1" />
         <Label Grid.Row="1" Grid.Column="0" />
         <TextBox Grid.Row="1" Grid.Column="1" />
      </apex:ApexGrid>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 2021-08-13
        • 1970-01-01
        • 1970-01-01
        • 2018-09-14
        相关资源
        最近更新 更多