Xamarin.Forms 将行插入网格并重新排列

Xamarin.Forms Insert Row to Grid and re-arrange(Xamarin.Forms 将行插入网格并重新排列)
本文介绍了Xamarin.Forms 将行插入网格并重新排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将新对象插入到第 0 行的现有网格中(将剩余的行向下移动一格).有没有办法做到这一点?与 Log 类似,最后一项位于第一位.请注意,我不能使用 ListView,我已经在内容中有一个.另外,我更喜欢使用 Grid,因为我可以更好地构建它以进行演示等.完成的网格结构:

I'm trying to insert new object into existing Grid at row 0 (shifting the remaining rows one lower). Is there a way to do it? Similarly to Log, last item goes in first position. Note that I cannot use ListView, I already have one in the content. Also, I'd prefer to use Grid as I can structure it better for presentation wise etc. Finished grid structure:

> <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),然后尝试使用 Child 插入顶行:

I'm generating the grid programmatically, to fill out the above structure (iterate column / rows nr), then attempt to insert top row with Child:

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

新行"将与原始行"重叠

"new row" will overlap "original row"

到目前为止,这就是我所做的.这仅适用于一次行移位,没有列移位.

So far, that's what I've done. This is only for one row shift,no column shift.

我无法通过 Grid Child 列/行获得

I could not get Grid Child column/row by

var left = Grid.Children[0].Left();//实验标志所以我将不得不进行更多迭代.

var left = Grid.Children[0].Left();//Experimental flag So I will have to iterate more.

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

... add new Rows with Labels, 0 column (By default, Grid has 1 Column, 1 Grid), then:

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

推荐答案

你可以把你的Label放到List中,然后实现.

You can put you Label to List, then achieve it.

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

For example, If I want add a label in the first row and second Column place, here is running 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>

这是我的布局背景代码.我在 List 中添加三个 Label,然后将 list 中的项目添加到 Grid.单击按钮时,在第 0 行第 1 列添加一个新的 Label,在 listview 中的位置为 1.

Here is my layout background code. I add three Labels in List, then add the items in list to the Grid. when click the Button, add a new Label to the row 0, column 1, the position in listview is 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);
        }
    }
}

这篇关于Xamarin.Forms 将行插入网格并重新排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)