如何在ListView中将网格嵌套到网格中

时间:2018-03-07 06:00:02

标签: c# xaml xamarin xamarin.forms xamarin.android

我在Xamarin的listview遇到了一些麻烦。我正在制作android应用程序,我有结构列表。在结构中有字段auto_num(汽车的编号),我必须通过该字段对列表的所有元素进行分组,并且我必须为auto_num字段具有相同值的所有其他行创建一个单元格。例如下面的照片:enter image description here

  

我怎么做?这是我的XAML:

        <ListView x:Name="deicerList" HasUnevenRows="True" ItemsSource="{Binding Antiicing}">
            <ListView.Header>
                <Grid x:Name="deicingGrid">
                    <Label Text="TYPE" Grid.Column="0" FontSize="17" ></Label>
                    <Label Text="QUANTITY" Grid.Column="1" FontSize="17"></Label>
                    <Label Text="REFRACT INDEX" Grid.Column="2" FontSize="17"></Label>
                    <Label Text="LABORATORY NO" Grid.Column="3" FontSize="17"></Label>
                    <Label Text="AUTO NO" Grid.Column="4" FontSize="17"></Label>
                </Grid>
            </ListView.Header>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                                <Grid x:Name="deicingGridParent" >
                                    <Entry Text="{Binding type}" Grid.Column="0" FontSize="17" ></Entry>
                                    <Entry Text="{Binding quantity}" Grid.Column="1" FontSize="17"></Entry>
                                    <Entry Text="{Binding rri}" Grid.Column="2" FontSize="17"></Entry>
                                    <Entry Text="{Binding analisys_number}" Grid.Column="3" FontSize="17"></Entry>
                                    <Entry Text="{Binding auto_num}" Grid.Column="4" FontSize="17"></Entry>
                                </Grid>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为使用Xamarin Forms很难......你可以尝试使用GropuHeader和&#34; Group&#34;您的商品使用auto_num字段

Xamarin文档中有tutorial

XAML:

    <?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="GroupingSampleListView.GroupedListXaml">
    <ContentPage.Content>
        <ListView x:Name ="lstView" IsGroupingEnabled="true" GroupDisplayBinding="{Binding LongName}" GroupShortNameBinding="{Binding ShortName}">
            <ListView.ItemTemplate>
                <DataTemplate><TextCell Text="{Binding Name}" Detail = "{Binding Comment}" /></DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>

CodeBehind

public partial class GroupedListXaml : ContentPage
{
    private ObservableCollection<GroupedVeggieModel> grouped { get; set; }
    public GroupedListXaml ()
    {
        InitializeComponent ();
        grouped = new ObservableCollection<GroupedVeggieModel> ();
        var veggieGroup = new GroupedVeggieModel () { LongName = "vegetables", ShortName="v" };
        var fruitGroup = new GroupedVeggieModel () { LongName = "fruit", ShortName = "f" };
        veggieGroup.Add (new VeggieModel () { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
        veggieGroup.Add (new VeggieModel () { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
        veggieGroup.Add (new VeggieModel () { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
        veggieGroup.Add (new VeggieModel () { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
        fruitGroup.Add (new VeggieModel () {Name = "banana", IsReallyAVeggie = false,Comment = "available in chip form factor"});
        fruitGroup.Add (new VeggieModel () {Name = "strawberry", IsReallyAVeggie = false,Comment = "spring plant"});
        fruitGroup.Add (new VeggieModel () {Name = "cherry", IsReallyAVeggie = false,Comment = "topper for icecream"});
        grouped.Add (veggieGroup); grouped.Add (fruitGroup);
        lstView.ItemsSource = grouped;
    }
}

&#34;代码&#34;

namespace GroupingSampleListView
{
    public class GroupedListCode : ContentPage
    {
        private ObservableCollection<GroupedVeggieModel> grouped { get; set; }

        public GroupedListCode ()
        {
            var lstView = new ListView ();
            grouped = new ObservableCollection<GroupedVeggieModel> ();
            var veggieGroup = new GroupedVeggieModel () { LongName = "vegetables", ShortName="v" };
            var fruitGroup = new GroupedVeggieModel () { LongName = "fruit", ShortName = "f" };
            veggieGroup.Add (new VeggieModel () { Name = "celery", IsReallyAVeggie = true, Comment = "try ants on a log" });
            veggieGroup.Add (new VeggieModel () { Name = "tomato", IsReallyAVeggie = false, Comment = "pairs well with basil" });
            veggieGroup.Add (new VeggieModel () { Name = "zucchini", IsReallyAVeggie = true, Comment = "zucchini bread > bannana bread" });
            veggieGroup.Add (new VeggieModel () { Name = "peas", IsReallyAVeggie = true, Comment = "like peas in a pod" });
            fruitGroup.Add (new VeggieModel () {Name = "banana", IsReallyAVeggie = false,Comment = "available in chip form factor"});
            fruitGroup.Add (new VeggieModel () {Name = "strawberry", IsReallyAVeggie = false,Comment = "spring plant"});
            fruitGroup.Add (new VeggieModel () {Name = "cherry", IsReallyAVeggie = false,Comment = "topper for icecream"});

            grouped.Add (veggieGroup); 
            grouped.Add (fruitGroup);

            lstView.ItemsSource = grouped;
            lstView.IsGroupingEnabled = true;
            lstView.GroupDisplayBinding = new Binding ("LongName");
            lstView.GroupShortNameBinding = new Binding ("ShortName");

            lstView.ItemTemplate = new DataTemplate (typeof(TextCell));
            lstView.ItemTemplate.SetBinding (TextCell.TextProperty, "Name");
//          lstView.ItemTemplate.SetBinding (TextCell.DetailProperty, "Comment");
            Content = lstView;
        }
    }
}

ViewModel

public class VeggieModel
{
    public string Name { get; set; }
    public string Comment { get; set; }
    public bool IsReallyAVeggie { get; set; }
    public string Image { get; set; }
    public VeggieModel ()
    {
    }
}

public class GroupedVeggieModel : ObservableCollection<VeggieModel>
{
    public string LongName { get; set; }
    public string ShortName { get; set; }
}