简体   繁体   English

如何从Xamarin表单中的ListView中删除项目

[英]How to delete item from ListView in Xamarin forms

I have a ListView and I want to delete some items, I have not found a useful answer yet. 我有一个ListView,我想删除一些项目,我还没有找到有用的答案。

this is a XMAL : 这是一个XMAL:

<ListView.ItemTemplate >
  <DataTemplate>
    <ViewCell>
      <StackLayout>
        <Label Text="{Binding Name}" 
               Style="{DynamicResource ListItemTextStyle}" />
        <Label Text="{Binding PhoneNo}" 
               Style="{DynamicResource ListItemDetailTextStyle}"/>
      </StackLayout>
    </ViewCell>
  </DataTemplate>
</ListView.ItemTemplate>
</ListView>

and listview : 和列表视图:

public ObservableCollection<Contact> ContactList2 { get; set; }

I can easily add it, but I do not know how to delete it. 我可以轻松添加它,但我不知道如何删除它。

Use a context menu 使用上下文菜单

XAML XAML

<ViewCell.ContextActions>
  <MenuItem Clicked="OnDelete" CommandParameter="{Binding .}"
           Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>

code behind 代码背后

public void OnDelete (object sender, EventArgs e) {
    var mi = ((MenuItem)sender);
    Contact contact = (Contact)mi.CommandParameter;
    ContactList2.Remove(contact);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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