Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.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# 表单将行插入网格并重新排列_C#_Xamarin.forms_Grid - Fatal编程技术网

C# 表单将行插入网格并重新排列

C# 表单将行插入网格并重新排列,c#,xamarin.forms,grid,C#,Xamarin.forms,Grid,我正在尝试将新对象插入到第0行的现有网格中(将其余行下移一行)。有办法吗?与日志类似,最后一项位于第一个位置。请注意,我不能使用ListView,我在内容中已经有一个。此外,我更喜欢使用网格,因为我可以更好地构建它,以便于演示等。 成品网格结构: > <Grid.RowDefinitions> > <RowDefinition Height="*"/> </Grid.RowDefinitions> > <G

我正在尝试将新对象插入到第0行的现有网格中(将其余行下移一行)。有办法吗?与日志类似,最后一项位于第一个位置。请注意,我不能使用ListView,我在内容中已经有一个。此外,我更喜欢使用网格,因为我可以更好地构建它,以便于演示等。 成品网格结构:

> <Grid.RowDefinitions>
>     <RowDefinition Height="*"/> </Grid.RowDefinitions>

> <Grid.ColumnDefinitions>
>      <ColumnDefinition/>
>      <ColumnDefinition/>
>      <ColumnDefinition/>  
</Grid.ColumnDefinitions>
> (existing Labels)
> <Label Text="1" Grid.Column="0" Grid.Row="0"/> 
<Label Text="2" Grid.Column="0" Grid.Row="0"/> 
> <Label Text="3", Grid.Column="0", Grid.Row="0"/>
>  </>

“新行”将与“原始行”重叠

编辑: 到目前为止,我就是这么做的。这仅适用于一行移位,不适用于列移位

我无法按获取网格子列/行

var left=Grid.Children[0].left()//实验标志
因此,我将不得不进行更多的迭代

。。。添加标签为0列的新行(默认情况下,网格有1列,1个网格),然后:


要将网格中的所有行移动一行,可以像下面的示例代码一样使用
grid.GetRow()
grid.SetRow()
方法

foreach (var child in grid.Children)
{
    var row = Grid.GetRow(child);
    Grid.SetRow(child, row + 1);
}

您可以将
标签
放入
列表
,然后实现它

例如,如果我想在第一行和第二列添加标签,这里运行的是GIF

这是我的网格布局

<StackLayout Margin="10">
      
        <Button Text="add" Clicked="Button_Clicked"></Button>
        <Grid x:Name="MyGrid">
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </Grid>
</StackLayout>

谢谢大家,非常有帮助。
Grid.RowDefinitions.Add(newRow); 

for (int i = Grid.Children.Count -1 ; i >= 0; i--) 
{ 
     var child = > Grid.Children[i]; 
     Grid.Children.RemoveAt(i); 
     Grid.Children.Add(child, 0, i +1); 
} 
Grid.Children.Add(SomeLabel, 0, 0);
foreach (var child in grid.Children)
{
    var row = Grid.GetRow(child);
    Grid.SetRow(child, row + 1);
}
<StackLayout Margin="10">
      
        <Button Text="add" Clicked="Button_Clicked"></Button>
        <Grid x:Name="MyGrid">
          <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
    </Grid>
</StackLayout>
public partial class MainPage : ContentPage
{
    List<Label> labels;

    public MainPage()
    {
        InitializeComponent();
        labels = new List<Label>();
        labels.Add(new Label
        {
            Text = "1",
          
            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        });
        labels.Add(new Label
        {
            Text = "2",

            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        });
        labels.Add(new Label
        {
            Text = "3",

            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        });

        for (int i=0; i<labels.Count ;i++)
        {
            //If we set the three columns, we can set the Column and  Row by i%3 i/3
            MyGrid.Children.Add(labels[i],i%3,i/3);
        }
    }
    
    private void Button_Clicked(object sender, EventArgs e)
    {
        // Remove the data in the original Gird
        for (int i = 0; i < labels.Count; i++)
        {
            if (MyGrid.Children.Count>0)
            {
                var label = labels[i];

                if (label != null)
                {
                    MyGrid.Children.Remove(label) ;
                }
            }
        }

        // add label
        var newLabel = new Label
        {
            Text = "new row",
            TextColor = Color.Black,
            LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
            HorizontalTextAlignment = TextAlignment.End,
            FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
        };

        // If I want add a label in the first row and second Column place, I can add the new label to with `Insert` method in List.
        labels.Insert(1,newLabel);

        // re-add the list data to grid.
        for (int i = 0; i < labels.Count; i++)
        {
            MyGrid.Children.Add(labels[i], i % 3, i / 3);
        }
    }
}