列表视图xamarin中项目的不同位置

时间:2019-12-17 10:15:26

标签: c# .net listview xamarin listviewitem

我正在尝试使用Listview。 首先,我用它来对齐列中的项目,它起作用了

现在,我正在尝试搜索解决方案以使我的项目在列中未对齐。

为此,我有一个8行8列的网格。

我想要第一个项目: Grid.Row="1" Grid.Column ="1" 我想要第二个项目: Grid.Row="3" Grid.Column ="3" 我想要第三个项目: Grid.Row="6" Grid.Column ="3" 我想要第四个项目: Grid.Row="8" Grid.Column ="1"

您认为有可能吗?你有什么主意吗? 我曾经阅读过ItemView和Listview文档,但没有找到有关网格位置的任何信息。

王者敬意

1 个答案:

答案 0 :(得分:0)

您需要自定义Grid。

网格

public class MyGird: Grid
{
    Label first;
    Label second;
    Label third;
    Label fourth;


    public MyGird()
    {
        this.BindingContextChanged += MyGird_BindingContextChanged;


        for(int i = 0; i < 8; i++)
        {
            RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
        }

        for (int i = 0; i < 8; i++)
        {
            ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
        }

        first = new Label { };
        second = new Label {  };
        third = new Label { };
        fourth = new Label {  };

        Children.Add(first, 0, 0);
        Children.Add(second, 2, 2);
        Children.Add(third, 5, 2);
        Children.Add(fourth, 7, 0);
    }

    private void MyGird_BindingContextChanged(object sender, EventArgs e)
    {
        var grid = sender as Grid;
        var obj = grid.BindingContext as List<string>;

        first.Text = obj[0];
        second.Text = obj[1];
        third.Text = obj[2];
        fourth.Text = obj[3];
    }
}

数据

 public List<string> list { get; set; }
    public Page2()
    {
        InitializeComponent();

        list = new List<string> { "1", "2", "3", "4" };

        this.BindingContext = this;
    }

用法

<local:MyGird BindingContext="{Binding list}"/>