C# Xamarin表单-如何在ListView中显示/绑定列表?
我正在创建一个购物车,在那里我存储所有订单,如何在ListView中显示列表 我试过做这些代码 CustomerOrder.csC# Xamarin表单-如何在ListView中显示/绑定列表?,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,我正在创建一个购物车,在那里我存储所有订单,如何在ListView中显示列表 我试过做这些代码 CustomerOrder.cs public class CustomerOrder { public string menuname { get; set; } public string price { get; set; } public string qty { get; set; } public string mco
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; }
}
公共类客户订单
{
公共字符串菜单名{get;set;}
公共字符串price{get;set;}
公共字符串数量{get;set;}
公共字符串mcode{get;set;}
}
公共类客户名单
{
//我想在Listview中显示/绑定此文件
公共列表客户_顺序{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>
如果MyCart是CustList的任何实例 在绑定中,可以{Binding CUSTOMER_ORDER.menuname}
你必须给它数据的路径 您可能缺少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));
}
}
公共类客户列表:INotifyPropertyChanged
{
//我想在Listview中显示/绑定此文件
私人名单(客户)订单
公开名单客户订单
{
获取{return\u CUSTOMER\u ORDER;}
设置{
_客户订单=价值;
OnProperty变更(“客户订单”);
}
}
公共事件属性更改事件处理程序属性更改;
受保护的虚拟void OnPropertyChanged(字符串propertyName)
{
PropertyChanged?调用(此,
新PropertyChangedEventArgs(propertyName));
}
}
还有一件事是,如果您缺少bindingcontext,您必须为xaml应用bindingcontext。我在您的示例中没有看到二维数组。代码丢失了吗?@SwapnilShah我编辑的我是说列表不是数组谢谢!为了更正。你如何给出路径?``就像这样?