Xamarin跨平台表与行和列

时间:2015-02-05 08:32:12

标签: c# xamarin

我想创建一个包含行和列的简单表:

C1 C2 C3 C4

R1 R1 R1 R1

R2 R2 R2 R2

等等。

起初我开始创建一个网格并为列和行分配标签,但很难对其进行管理并执行过滤,重新排列和删除行等操作。我还能做什么?

编辑: 为了删除行,我使用了一个字典,其中每个行和行号代表一个值。之后我才使用layout.Children.RemoveAt(rowNumber)。问题是其他行没有重新定位,它们之间存在间隙。我想我必须重新制作每行删除的布局。

1 个答案:

答案 0 :(得分:1)

试试这个,它可能是最简单的方法,创建一个ViewItem和一个ViewModel项,视图将是这样的:

public class NearMeView : ContentPage
    {
        private ListView _listView;
        private MyViewModel _viewModel;
        public NearMeView ()
        {
            _viewModel = new MyViewModel ();
            BindingContext = _viewModel;
            _listView = new ListView (){
                ItemTemplate = CreateItemTemplate ()//Here you will set th data templates for each row and in each label nameLabel.SetBinding<Item> (Label.TextProperty, x => x.Name);
            };
            _listView.SetBinding<MyViewModel> (ListView.ItemsSourceProperty, x => x.Rows);
}

public class MyViewModel : ViewModel
{
     List<Item>Rows;//will be populated from your DB each item will represent a Row, so you just need to remove from this list the item and automatically from the View should be removed the row.
    // Method that you need to remove filter etc.
}
private DataTemplate CreateItemTemplate ()
        {//this metod create the layout that you want to apply for each row in this example there are 3 columns, in each column there is a label, each label is binded to an element of the ItemSourceProperty of the ListView
            return new DataTemplate (() => {
                var nameLabel = new Label ();
                var typeLabel = new Label ();
                var distanceLabel = new Label (){   HorizontalOptions = LayoutOptions.EndAndExpand,VerticalOptions = LayoutOptions.Center   };
                nameLabel.SetBinding<BranchModel> (Label.TextProperty, x => x.Name);
                typeLabel.SetBinding<BranchModel> (Label.TextProperty, x => x.Type);
                distanceLabel.SetBinding<BranchModel> (Label.TextProperty,x=> x.Distance);
                var leftStack = new StackLayout (){
                    Orientation = StackOrientation.Vertical,
                    VerticalOptions = LayoutOptions.Center,
                    Children = { nameLabel, typeLabel },
                    Padding = new Thickness (8,0,0,0)

                };
                return new ViewCell () {
                    View = new StackLayout () {
                        Orientation = StackOrientation.Horizontal,
                        Spacing = 5,
                        Children = { leftStack, distanceLabel },
                        Padding = new Thickness (0,0,0,8)
                    }
                };
            });
        }

这几乎是我用来使用SearchBar过滤列表的代码