简体   繁体   中英

Xamarin Forms - Delete Row from ListView and Observable Collection

I have a simple question. How do you delete a row from ListView that is binded to an observable collection?

Here's my XAML code:

<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 code

public ViewShoppingCart()
        {
            InitializeComponent();

            MyListView.ItemsSource = ShoppingCart.fbproducts;
        }


private void DeleteAction(object sender, System.EventArgs e)
        {
// something goes here
        }

Assuming ShoppingCart.fbproducts will hold the same instance of the ObservableCollection and not recreating it. all you need to do is

ShoppingCart.fbproducts.Remove(ItemYouWantToRemove)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM