Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

listView binding

$
0
0

How to Bind the ListView with an Observablecollection type where the type itself an observable collection.
My MVVM like below

ViewModel

 public  class OrdersViewModel
{
    public ObservableCollection<Order> Orders{ get; set; }

    public OrdersViewModel()
    {

        Orders = new ObservableCollection<Order> { new Order(),new Order(),new Order()};
    }


}

Models:

public class Order:INotifyPropertyChanged
{
    public ObservableCollection<OrderItem> OrderItems { get;  set; }


    public Order()
    {
        OrderItems = new ObservableCollection<OrderItem> { new OrderItem("xxxx", "2", "200"),
                                                            new OrderItem("yyyy", "1", "500"),
                                                            new OrderItem("zzzz", "1", "400")
                                                           };
        OrderId =( new Guid()).ToString();

    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var changed = PropertyChanged;
        if(changed != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class OrderItem
{

    public string OrderITemName { get;  set; }

    public string Units { get; set; }

    public  string UnitPricePerItem { get; set; }

    public OrderItem(string _orderITemName,string _units,string  _unitPricePerItem)
    {
        OrderITemName = _orderITemName;
        Units = _units;
        UnitPricePerItem = _unitPricePerItem;
    }

}

View

<ContentPage.BindingContext>
    <local:OrdersViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>        
    <StackLayout>          
        <ListView  x:Name="OrdersListView"  ItemsSource="{Binding  Orders}"  >
            <ListView.ItemTemplate >
                <DataTemplate >
                    <ViewCell>
                        <StackLayout>
                            <Label x:Name="OrderITemName" Text="{Binding OrderITemName}" />
                            <Label x:Name="Units" Text="{Binding Units}" />
                            <Label x:Name="UnitPricePerItem" Text="{Binding UnitPricePerItem}" />
                        </StackLayout>                           
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>          
</ContentPage.Content>

Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>