[英]Xamarin Forms - how to display/Bind List in ListView?
我正在創建用於存儲所有訂單的購物車,如何在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事件。
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。
如果MyCart是CustList的任何實例,
在綁定中,您可以{Binding CUSTOMER_ORDER.menuname}。
您必須為其提供數據路徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.