【发布时间】:2017-07-10 16:50:16
【问题描述】:
喂!
我想创建一个 ObjectListView,您可以在其中使用 ContextMenu 删除项目。
所以基本上我曾经通过获取 OLV.SelectedIndex 来删除它,然后从 OLV 所基于的列表中删除,并重新设置 OLV 对象。 然后我意识到,如果我对OLV进行排序,然后删除一个项目,它会删除另一个项目,因为选定的项目索引不等于列表中的索引。
通过 OLV CellRightClick 事件,我可以获得被点击项目(e.Model)背后的对象,但我不知道如何将其传递给 ContextMenu 点击事件处理程序。
主题是一个列表。
private void subjectListView_CellRightClick(object sender, BrightIdeasSoftware.CellRightClickEventArgs e)
{
if (subjectsListView.SelectedIndex != -1)
{
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("Delete", new EventHandler(DeleteItem));
subjectsListView.ContextMenu = cm;
}
}
void DeleteItem(object sender, EventArgs e)
{
//get the Subject object, which was clicked on
Subjects.RemoveAt(subjectsListView.SelectedIndex);
subjectsListView.SetObjects(Subjects);
}
所以基本上我想在单击 ContextMenus“删除”项时获取对象(而不是索引)。 另外,我觉得有一种更简单的方法可以做到这一点。
感谢您的回答。
【问题讨论】:
标签: c# events contextmenu objectlistview