猿问

Xamarin Forms - 如何在 ListView 中显示/绑定列表?

我正在创建一个存储所有订单的购物车,如何在 ListView 中显示列表?


我试着做这些代码


客户订单.cs


public class CustomerOrder

    {

        public string menuname { get; set; }

        public string price { get; set; }

        public string qty { get; set; }

        public string mcode { get; set; }

    }


    public class CustList

    {

        //i want to display/bind this in Listview

        public List<CustomerOrder> CUSTOMER_ORDER { get; set; }

    }

OrderCart.xaml


<ListView x:Name="MyCart" ItemSelected="MyCart_ItemSelected"  ItemsSource="{Binding CUSTOMER_ORDER}" RowHeight="50">

        <ListView.ItemTemplate>

            <DataTemplate>

                <ViewCell >

                    <Grid>


                                <StackLayout Orientation="Horizontal">

                                    <Label  Text="{Binding menuname}" Font="30" TextColor="Black" FontAttributes="Bold"/>

                                    <Label  Text="{Binding qty}" Font="30" TextColor="Black" FontAttributes="Bold"/>

                                    <Label  Text="{Binding price}" Font="30" TextColor="Black" FontAttributes="Bold"/>

                                    <Label  Text="{Binding mcode}" Font="30" TextColor="Black" FontAttributes="Bold"/>

                                </StackLayout>

                  </Grid>

                </ViewCell>

            </DataTemplate>

        </ListView.ItemTemplate>


    </ListView>


守着一只汪
浏览 359回答 2
2回答

手掌心

您可能会错过 PropertyChanged 事件。public class CustList:INotifyPropertyChanged&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //i want to display/bind this in Listview&nbsp; &nbsp; &nbsp; &nbsp; private List<CustomerOrder> _CUSTOMER_ORDER&nbsp; &nbsp; &nbsp; &nbsp; public List<CustomerOrder> CUSTOMER_ORDER&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; get{return _CUSTOMER_ORDER;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; set{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _CUSTOMER_ORDER=value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OnPropertyChanged("CUSTOMER_ORDER");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public event PropertyChangedEventHandler PropertyChanged;&nbsp; &nbsp; &nbsp; &nbsp; protected virtual void OnPropertyChanged(string propertyName)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PropertyChanged?.Invoke(this,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new PropertyChangedEventArgs(propertyName));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }还有一件事是,如果您缺少它,您必须为您的 xaml 应用 bindingcontext。

繁星淼淼

如果 MyCart 是 CustList 的任何实例,在您的绑定中,您可以{Binding CUSTOMER_ORDER.menuname}。您必须为其提供数据路径。
随时随地看视频慕课网APP
我要回答