using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class FormListView : Form
{
public FormListView()
{
InitializeComponent();
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
ContextMenuStrip menu = sender as ContextMenuStrip;
//初始化tag属性,因ListView只有两个分组,menu.tag对应为0,1
for (int i = 0; i < menu.Items.Count; i++)
{
menu.Items[i].Tag = i % 2;
}
//设置ListViewGroup的tag为0,1,……
for (int i = 0; i < listView1.Groups.Count; i++)
{
listView1.Groups[i].Tag = i;
}
Dictionary<object, bool> dict = new Dictionary<object, bool>();
foreach (ListViewItem lvi in listView1.SelectedItems)
{
dict[lvi.Group.Tag] = true;
}
//ContextMenuStrip menu = sender as ContextMenuStrip;
foreach (ToolStripMenuItem mi in menu.Items)
{
mi.Visible = dict.ContainsKey(mi.Tag);
}
}
}
}
------解决方案--------------------