首页 > 解决方案 > 列表包括另一个列表 xamarin 表单

问题描述

我在另一个列表中实现列表时遇到问题,我有项目列表,每个项目都有尺寸、数量和价格列表。用户选择大小并输入数量然后选择父项,如何在 xamarin 表单中实现?

我们数据中的示例

标签: xamlxamarin.forms

解决方案


您可以尝试使用带有嵌套 CollectionView 或 ListView 的可绑定布局,这将允许您使用列表列表进行数据绑定。

它可能看起来像这样;

C#

public class MyNestedListItem {
    
    public MyNestedListItem(string myTest)  {
        MyText = myText;
    }

    public string MyText { get; set; }
}


public class MyList {

    public MyList(string listName, list<MyNestedListItem> myNestedList) {
        ListName = listName;
        MyNestedList = myNestedList; 
    }
    
    public string ListName { get; set; }
    public List<MyNestedListItem> MyNestedList { get; set; }
}


public class MyViewModel {
    
    public ObservableCollection<MyList> MyList { get; set }

    public MyViewModel() {

        var nestedListA = new List<MyNestedListItem> 
        { 
            new MyNestedListItem("A1"); 
            new MyNestedListItem("A2"); 
            new MyNestedListItem("A3"); 
        }

        var nestedListB = new ...;


        MyList = new ObservableCollection<MyList> {
            new MyList("List A", nestedListA);
            new MyList("List B", nestedListB);
        } 
    }
}

XAML

<StackLayout Orientation="Horizontal"
        VerticalOptions="Start"
        HorizontalOptions="Center"
        BindableLayout.ItemsSource="{Binding MyList}">
<BindableLayout.ItemTemplate>
    <DataTemplate>
        <Frame WidthRequest="75" 
            HeightRequest="25"
            CornerRadius="25"
            Padding="0"
            HasShadow="True">
        <Label Text="{Binding ListName }"
            <CollectionView ItemsSource="{Binding MyNestedList }" 
                VerticalOptions="Center"
                HorizontalOptions="Center">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                    < ..... your layout stuff here />
                    </StackLayout>
                </DataTemplate>
            </CollectionView>
        </Frame>
    </DataTemplate>
</BindableLayout.ItemTemplate>

这可能是实现嵌套列表的良好起点


推荐阅读