Xamarin Forms - удаление строки из ListView и наблюдаемой коллекции - PullRequest
0 голосов
/ 06 августа 2020

У меня простой вопрос. Как удалить строку из 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
        }

1 Ответ

1 голос
/ 06 августа 2020

Предполагается, что ShoppingCart.fbproducts будет содержать тот же экземпляр ObservableCollection и не будет его воссоздавать. все, что вам нужно сделать, это

ShoppingCart.fbproducts.Remove(ItemYouWantToRemove)
...