我正在尝试使用ContextActions从组列表视图中删除一个项目;但是,当我单击它时,它什么也没做
我知道如何从列表视图中删除一个项目,但不删除属于列表视图中的组的项目
This is #CODE side of the code
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using XamarinForm3.MODELSID;
namespace XamarinForm3
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(true)]
public partial class MainPage : ContentPage
{
private ObservableCollection<ContactIDGroup> _contactsID; //List<ContactIDGroup> will not be able to update the screen
public MainPage()
{
InitializeComponent();
_contactsID = new ObservableCollection<ContactIDGroup> {
new ContactIDGroup("V", "V")
{
new ContactID {Name="Victor", ImageUrl="http://lorempixel.com/100/100/people/1" }
},
new ContactIDGroup("J", "J")
{
new ContactID {Name="Jon", ImageUrl="http://lorempixel.com/100/100/people/2", Status="Hello"}
}
};
listViewID.ItemsSource = _contactsID;
}
void Handle_ItemSelected(object sender, Xamarin.Forms.SelectedItemChangedEventArgs e)
{
var contact = e.SelectedItem as ContactID;
DisplayAlert("Selected", contact.Name, "OK");
}
void Handle_ItemTapped(object sender, Xamarin.Forms.ItemTappedEventArgs e)
{
var contact = e.Item as ContactID;
DisplayAlert("Tapped", contact.Name, "OK");
}
void CallID_Clicked(object sender, System.EventArgs e)
{
var menuItemID = sender as MenuItem;
var contactID = menuItemID.CommandParameter as ContactID;
DisplayAlert("CallID", contactID.Name, "OK");
}
void DeleteID_Clicked(object sender, System.EventArgs e)
{
var menuItemID = sender as MenuItem;
var contactID = menuItemID.CommandParameter as ContactIDGroup;
_contactsID.Remove(contactID);
}
}
}
This XAML side
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:XamarinForm3" x:Class="XamarinForm3.MainPage">
<ListView x:Name="listViewID"
IsGroupingEnabled="true"
GroupDisplayBinding="{Binding TitleID}"
GroupShortNameBinding="{Binding ShortTitleID}"
ItemSelected="Handle_ItemSelected"
ItemTapped="Handle_ItemTapped">
<ListView.ItemTemplate>
<DataTemplate>
<ImageCell Text="{Binding Name}" Detail="{Binding Status}" ImageSource="{Binding ImageUrl}">
<ImageCell.ContextActions>
<MenuItem Text="CallID" Clicked="CallID_Clicked" CommandParameter="{Binding .}"/>
<MenuItem Text="DeleteID" Clicked="DeleteID_Clicked" IsDestructive="true" CommandParameter="{Binding .}"/>
</ImageCell.ContextActions>
</ImageCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
This is ContactID class
using System;
namespace XamarinForm3.MODELSID
{
public class ContactID
{
public string Name { get; set; }
public string Status { get; set; }
public string ImageUrl { get; set; }
}
}
This is ContactIDGroup class
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace XamarinForm3.MODELSID
{
public class ContactIDGroup : ObservableCollection<ContactID>
{
public string TitleID { get; set; }
public string ShortTitleID { get; set; }
public ContactIDGroup(string titleID, string shortTitleID)
{
TitleID = titleID;
ShortTitleID = shortTitleID;
}
}
}
我想要的是当我按下MenuItem中的Delete选项时,它实际上消失了。
答案 0 :(得分:0)
尝试一下:
var contact = (sender as VisualElement).BindingContext as ContactID;
答案 1 :(得分:0)
原因:
var contactID = menuItemID.CommandParameter
将返回 ContactID ,而不是 ContactIDGroup
解决方案:
请参阅以下代码
void DeleteID_Clicked(object sender, System.EventArgs e)
{
var menuItemID = sender as MenuItem;
var contactID = menuItemID.CommandParameter as ContactID;
for (int i=0;i<_contactsID.Count;i++)
{
ContactIDGroup contactIDs = _contactsID[i];
for (int j = 0; j < contactIDs.Count; j++)
{
if (contactIDs[j] == contactID )
{
_contactsID[i].RemoveAt(j);
}
}
}
}
答案 2 :(得分:0)
private void DeleteClicked(object sender, EventArgs e)
{
var contactId = (sender as MenuItem).CommandParameter as ContactId;
for(int i = 0; i < _contactsId.Count; i++)
{
for(int j = 0; j<_contactsId[i].Count; j++)
{
if(_contactsId[i][j] == contactId)
{
_contactsId[i].Remove(contactId);
}
}
}
}