Xamarin Forms-如何在ListView中显示/绑定列表? [英] Xamarin Forms - how to display/Bind List in ListView?

查看:95
本文介绍了Xamarin Forms-如何在ListView中显示/绑定列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm creating a cart where i store all of my orders , How to display List in ListView?

我尝试执行这些代码

CustomerOrder.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>

推荐答案

您可能会缺少PropertyChanged事件.

You might missing PropertyChanged event.

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.

And one more thing is you have to apply bindingcontext for your xaml if you are missing that.

这篇关于Xamarin Forms-如何在ListView中显示/绑定列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆