Xamarin Forms - 从 ListView 和 Observable 集合中删除行
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>
CS 码
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)
我有一个简单的问题。如何从 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>
CS 码
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)