为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
守着一只汪 2022-01-09 10:47:52
我正在创建一个存储所有订单的购物车,如何在 ListView 中显示列表?我试着做这些代码客户订单.cspublic 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>
查看完整描述

2 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

您可能会错过 PropertyChanged 事件。


public class CustList:INotifyPropertyChanged

    {

        //i want to display/bind this in Listview

        private List<CustomerOrder> _CUSTOMER_ORDER

        public List<CustomerOrder> CUSTOMER_ORDER 

        { 

          get{return _CUSTOMER_ORDER;}

          set{

              _CUSTOMER_ORDER=value;

              OnPropertyChanged("CUSTOMER_ORDER");

             } 

        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)

        {

            PropertyChanged?.Invoke(this, 

            new PropertyChangedEventArgs(propertyName));

        }

    }

还有一件事是,如果您缺少它,您必须为您的 xaml 应用 bindingcontext。


查看完整回答
反对 回复 2022-01-09
?
繁星淼淼

TA贡献1775条经验 获得超11个赞

如果 MyCart 是 CustList 的任何实例,

在您的绑定中,您可以{Binding CUSTOMER_ORDER.menuname}。

您必须为其提供数据路径。


查看完整回答
反对 回复 2022-01-09
  • 2 回答
  • 0 关注
  • 358 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号