简体   繁体   English

Xamarin表单中的可编辑分组ListView

[英]Editable Grouped ListView in Xamarin Forms

I Would like to display something like nested listview in Xamarin forms. 我想以Xamarin形式显示类似嵌套listview的内容。 I mean i would like to have list of Person with theirs orders (the list of order could be empty). 我的意思是我想将Person列表与他们的订单一起显示(订单列表可以为空)。 The List would allow to add order to specific person, remove person (and all of his orders), and remove specific order. 该列表将允许向特定人员添加订单,移除人员(及其所有订单)以及移除特定订单。

在此处输入图片说明

I Considerd to create ListView of OrderModel where OrderModel would looks like: 我考虑过创建OrderModel的 ListView,其中OrderModel看起来像:

public class OrderModel
{
    public string PersonName {get;set;}
    public Order Order {get; set;}
}

But I dont know if this is a good idea, and how to implement something like this. 但是我不知道这是否是一个好主意,以及如何实现这样的东西。

I write a simple demo to achieve your requirement. 我编写了一个简单的演示来满足您的要求。 I use the Grouped ListViews : 我使用Grouped ListViews

OrderModel : OrderModel

public class OrderModel
{

    public string orderName { get; set; }

    public OrderModel()
    {
    }
}

public class GroupedOrderModel : ObservableCollection<OrderModel>
{
    public string personName { get; set; }
}

Set the dataSource: 设置数据源:

public partial class GroupedListXaml : ContentPage
{
    private ObservableCollection<GroupedOrderModel> grouped { get; set; }
    public GroupedListXaml ()
    {
        InitializeComponent ();
        grouped = new ObservableCollection<GroupedOrderModel> ();
        var person1Group = new GroupedOrderModel() { personName = "   john"};
        var person2Group = new GroupedOrderModel() { personName = "   alex"};
        var person3Group = new GroupedOrderModel() { personName = "   jack"};


        person1Group.Add (new OrderModel () { orderName = "   OrderOne"});
        person1Group.Add (new OrderModel() { orderName = "   OrderTwo" });
        person1Group.Add (new OrderModel() { orderName = "   OrderThree"});
        person1Group.Add (new OrderModel() { orderName = "   OrderFour"});

        person2Group.Add (new OrderModel() { orderName = "   OrderOne"});
        person2Group.Add (new OrderModel() { orderName = "   OrderTwo"});
        person2Group.Add (new OrderModel() { orderName = "   OrderThree"});

        grouped.Add (person1Group);
        grouped.Add (person2Group);

        //Person3 without OrderModel
        grouped.Add(person3Group);

        lstView.ItemsSource = grouped;
    }
}

In XAML , use ListView.GroupHeaderTemplate to customize the group header: 在XAML中 ,使用ListView.GroupHeaderTemplate自定义组头:

<ContentPage.Content>
    <ListView x:Name ="lstView" IsGroupingEnabled="true" Footer="">

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding orderName}" VerticalOptions="Center"  HorizontalOptions="StartAndExpand"/>
                        <Button Text="remove   " HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"/>
                    </StackLayout>
                </ViewCell>

            </DataTemplate>
        </ListView.ItemTemplate>


        <ListView.GroupHeaderTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding personName}" VerticalOptions="Center"  HorizontalOptions="StartAndExpand"/>
                        <Button Text="remove all   " TextColor="Red" HorizontalOptions="EndAndExpand" VerticalOptions="CenterAndExpand"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.GroupHeaderTemplate>

    </ListView>
</ContentPage.Content>

Let's have a look at result: 让我们看一下结果:

分组的ListViews

how can I display person3 if his items list is empty? 如果person3的项目列表为空,如何显示person3?

Check the dataSource code and Person3 has no orderModel . 检查dataSource代码,Person3没有orderModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM