Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在xaml中创建多个行和列_C#_Wpf - Fatal编程技术网

C# 在xaml中创建多个行和列

C# 在xaml中创建多个行和列,c#,wpf,C#,Wpf,为了正确管理自定义控件,我需要创建大量的行和列。因此,我的问题是,是否有可能实现与下面显示的代码相同的结果?以更干净的方式,这感觉很不现实 <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition>&

为了正确管理自定义控件,我需要创建大量的行和列。因此,我的问题是,是否有可能实现与下面显示的代码相同的结果?以更干净的方式,这感觉很不现实

<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>

您可以在代码隐藏中执行相同的操作,但通常您应该像以前一样在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
            });
        }
//添加10行
对于(int i=0;i<10;i++)
{
var height=GridLength.Auto;
如果(i==0)
高度=新的GridLength(1,GridUnitType.Star);
layoutGrid.RowDefinitions.Add(新的RowDefinition()
{
高度=高度
});    
}
//添加7列
对于(int i=0;i<7;i++)
{
var-width=GridLength.Auto;
如果(i==0)
宽度=新的GridLength(1,GridUnitType.Star);
layoutGrid.ColumnDefinitions.Add(新建ColumnDefinition()
{
宽度=宽度
});
}
您可以查看以下语法:

<AutoGrid RowCount="2" RowHeight="35" Columns="100,auto">
  <Label />
  <TextBox />
  <Label />
  <TextBox />
</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>


不,那是唯一的办法。您可以重新组织页面,或者使用一些用户控件吗?或者您可以对网格进行子类化,并添加所需行数和列数的属性。您可以使用
来缩短定义。您是否需要定义定义的宽度和高度,还是应该始终使用默认值?