【问题标题】:different position of items in a listview xamarinlistview xamarin中项目的不同位置
【发布时间】:2019-12-17 10:15:26
【问题描述】:

我正在尝试使用 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 文档,但我没有找到任何关于网格位置的信息。

国王的问候

【问题讨论】:

    标签: c# .net listview xamarin listviewitem


    【解决方案1】:

    您需要自定义 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}"/>
    

    【讨论】:

    • 你说得对,非常感谢这个例子,我现在知道 xamarin 中渲染器的原理,非常感谢
    猜你喜欢
    • 2013-04-23
    • 2016-08-04
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 2020-06-19
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多