gpt4 book ai didi

c# - Xamarin Forms - 如何在 ListView 中显示/绑定(bind)列表?

转载 作者:行者123 更新时间:2023-12-02 17:31:29 25 4
gpt4 key购买 nike

我正在创建一个购物车,用于存储所有订单,如何在 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 应用绑定(bind)上下文。

关于c# - Xamarin Forms - 如何在 ListView 中显示/绑定(bind)列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52231810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com