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

C# 如何在ListView中将网格嵌套到网格中,c#,xaml,xamarin,xamarin.forms,xamarin.android,C#,Xaml,Xamarin,Xamarin.forms,Xamarin.android,我在Xamarin的listview中遇到一些问题。我正在制作android应用程序,我有一个结构列表。在结构中有一个字段auto_num(一辆汽车的编号),我必须根据它对列表的所有元素进行分组,并且必须为auto_num字段具有相同值的所有其他行创建一个单元格。如下图所示: 我怎么能做到?这是我的XAML: 我认为Xamarin的形式很难。。。您可以尝试使用GropuHeader并使用auto_num字段“分组”您的项目 关于Xamarin文档有一个详细的说明 XAML: <

我在Xamarin的listview中遇到一些问题。我正在制作android应用程序,我有一个结构列表。在结构中有一个字段auto_num(一辆汽车的编号),我必须根据它对列表的所有元素进行分组,并且必须为auto_num字段具有相同值的所有其他行创建一个单元格。如下图所示:

我怎么能做到?这是我的XAML:



我认为Xamarin的形式很难。。。您可以尝试使用GropuHeader并使用auto_num字段“分组”您的项目

关于Xamarin文档有一个详细的说明

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>

代码隐藏

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;
    }
}
public分部类GroupedListXaml:ContentPage
{
私有ObservableCollection分组{get;set;}
公共GroupedListXaml()
{
初始化组件();
分组=新的ObservableCollection();
var vegigegegroup=newgroupedvegigemodel(){LongName=“vegets”,ShortName=“v”};
var fruitGroup=newgroupedvegiemodel(){LongName=“fruit”,ShortName=“f”};
添加(newvegiemodel(){Name=“芹菜”,IsReallyAVeggie=true,Comment=“尝试日志上的蚂蚁”});
添加(newvegiemodel(){Name=“西红柿”,isreallyveggie=false,Comment=“与罗勒搭配良好”});
添加(newvegiemodel(){Name=“zucchini”,IsReallyAVeggie=true,Comment=“zucchinibread>bannanabread”});
添加(newvegiemodel(){Name=“peas”,IsReallyAVeggie=true,Comment=“like peas in a pod”});
Add(newvegiemodel(){Name=“banana”,IsReallyAVeggie=false,Comment=“可在芯片形状因子中获得”});
添加(newvegiemodel(){Name=“草莓”,isreallyveggie=false,Comment=“spring plant”});
添加(newvegiemodel(){Name=“cherry”,isreallyveggie=false,Comment=“topper for icecream”});
grouped.Add(蔬菜组);grouped.Add(水果组);
lstView.ItemsSource=已分组;
}
}
“守则”

名称空间分组示例列表视图
{
公共类GroupedListCode:ContentPage
{
私有ObservableCollection分组{get;set;}
公共GroupedListCode()
{
var lstView=newlistview();
分组=新的ObservableCollection();
var vegigegegroup=newgroupedvegigemodel(){LongName=“vegets”,ShortName=“v”};
var fruitGroup=newgroupedvegiemodel(){LongName=“fruit”,ShortName=“f”};
添加(newvegiemodel(){Name=“芹菜”,IsReallyAVeggie=true,Comment=“尝试日志上的蚂蚁”});
添加(newvegiemodel(){Name=“西红柿”,isreallyveggie=false,Comment=“与罗勒搭配良好”});
添加(newvegiemodel(){Name=“zucchini”,IsReallyAVeggie=true,Comment=“zucchinibread>bannanabread”});
添加(newvegiemodel(){Name=“peas”,IsReallyAVeggie=true,Comment=“like peas in a pod”});
Add(newvegiemodel(){Name=“banana”,IsReallyAVeggie=false,Comment=“可在芯片形状因子中获得”});
添加(newvegiemodel(){Name=“草莓”,isreallyveggie=false,Comment=“spring plant”});
添加(newvegiemodel(){Name=“cherry”,isreallyveggie=false,Comment=“topper for icecream”});
分组。添加(蔬菜组);
分组。添加(水果组);
lstView.ItemsSource=已分组;
lstView.IsGroupingEnabled=true;
lstView.GroupDisplayBinding=新绑定(“LongName”);
lstView.GroupShortNameBinding=新绑定(“ShortName”);
lstView.ItemTemplate=新数据模板(typeof(TextCell));
lstView.ItemTemplate.SetBinding(TextCell.TextProperty,“名称”);
//lstView.ItemTemplate.SetBinding(TextCell.DetailProperty,“注释”);
内容=视图;
}
}
}
视图模型

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; }
}
公共类蔬菜模型
{
公共字符串名称{get;set;}
公共字符串注释{get;set;}
公共场所是真正的素食者{get;set;}
公共字符串图像{get;set;}
公共蔬菜模型()
{
}
}
公共类GroupedVeggieModel:ObservableCollection
{
公共字符串LongName{get;set;}
公共字符串短名称{get;set;}
}

可编辑?在GroupHeader上打开一个条目控件?例如在前面的照片中,我必须将红色边框中的自动单元格合并为界面上的一个单元格我认为您可以定义您的GroupTemplate,这样当您单击它时,您可以弹出一个条目并修改值。。。
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;
        }
    }
}
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; }
}