![](/img/trans.png)
[英]Having trouble binding an Observable collection to my ListView in Xamarin Forms
[英]Xamarin Forms - Delete Row from ListView and Observable Collection
我有一个简单的问题。 如何从绑定到可观察集合的 ListView 中删除一行?
这是我的 XAML 代码:
<ListView x:Name="MyListView" ItemsSource="{Binding products}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem ClassId="{Binding ProductName}" Clicked="DeleteAction" Text="Delete" IsDestructive="true" CommandParameter="{Binding .}" />
</ViewCell.ContextActions>
<StackLayout Orientation="Horizontal" Spacing="10">
<Image Source="{Binding ProductImage}" />
<Label Text="{Binding ProductName}" />
<Label Text="{Binding ProductPrice, StringFormat='{0:C}'}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
客户服务代码
public ViewShoppingCart()
{
InitializeComponent();
MyListView.ItemsSource = ShoppingCart.fbproducts;
}
private void DeleteAction(object sender, System.EventArgs e)
{
// something goes here
}
假设ShoppingCart.fbproducts
将拥有相同的ObservableCollection
实例,而不是重新创建它。 你需要做的就是
ShoppingCart.fbproducts.Remove(ItemYouWantToRemove)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.