2

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

i tried doing these codes

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>
2
  • I don’t see a two dimensional array in your sample. Is there code missing? Commented Sep 8, 2018 at 5:21
  • @SwapnilShah i editted it i meant List not array thank you! for the correction. Commented Sep 8, 2018 at 6:07

2 Answers 2

4

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));
        }
    }

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

Sign up to request clarification or add additional context in comments.

Comments

1

If MyCart is any instance of CustList,

In your bindings, you can {Binding CUSTOMER_ORDER.menuname}.

You have to give it path to the data.

3 Comments

how do you give path?
` <ListView x:Name="MyCart" ItemsSource="{Binding CustList}" >`
just like this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.