繁体   English   中英

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>

客户服务代码

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.

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