提问者:小点点

沙马林。表格将行插入网格并重新排列


我正在尝试将新对象插入到第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"/>
>  </>

我正在以编程方式生成网格,以填写上述结构(迭代列/行nr),然后尝试插入顶部行与儿童:

MyGrid.RowDefinitions.Insert(0,newDefinition); // Insert new row

               

>  MyGrid.Children.Add(new Label
>                         {
>               Text = "original row",
>                             TextColor = Color.Black,
>                             LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
>                             HorizontalTextAlignment = TextAlignment.End,
>                             FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
>                         }, 0, 0); //Column / Row

...

> MyGrid.RowDefinitions.Insert(0,newDefinition); // Insert new row
> at 0 row index
> 
> 
>                         MyGrid.Children.Add(new Label
>                         {
>               Text = "new row",
>                             TextColor = Color.Black,
>                             LineBreakMode = Xamarin.Forms.LineBreakMode.WordWrap,
>                             HorizontalTextAlignment = TextAlignment.End,
>                             FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)),
>                         }, 0, 0); //Column / Row

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

编辑:到目前为止,这就是我所做的。这只适用于一行移位,没有列移位。

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

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

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

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

共2个答案

匿名用户

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

例如,如果我想在第一行和第二列位置添加一个标签,这里是运行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>

这是我的布局背景代码。我在列表中添加三个标签,然后将列表中的项目添加到网格中是1。

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);
        }
    }
}

匿名用户

要将网格中的所有行移位一个,请使用Grid。GetRow()网格。SetRow()方法可以像下面的示例代码一样使用。

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