自己写了一个 程序菜单管理类
在调用 DeleteItem 方法时 不能删除 菜单项目。这个功能关系到程序的整体构架
public class LcdMenuManager:IlcdMenu
{
Menu lcdMenu;
public Menu LcdMenu
{
get { return lcdMenu; }
set {lcdMenu=value;}
}
Dictionary<string, MenuItem> menuItems = new Dictionary<string, MenuItem>();
public MenuItem GetMenuItem(string header)
{
if (!menuItems.Keys.Contains(header))
return null;
MenuItem mi = menuItems[header];
return mi;
}
public MenuItem AddItem(string father, string header, RoutedEventHandler handler)
{
MenuItem mi = new MenuItem();
mi.Header = header;
mi.Name = header;
if (handler != null)
mi.Click += handler;
menuItems.Add(header, mi);
if (!string.IsNullOrEmpty(father))
{
MenuItem fr = GetMenuItem(father);
fr.Items.Add(mi);
return fr;
}
lcdMenu.Items.Add(mi);
return mi;
}
//下面的函数不能 删除 菜单项目
public LcdMenuManager DeleteItem(string header)
{
MenuItem mi = GetMenuItem(header);
if (mi != null)
{
mi.Items.Clear();
lcdMenu.Items.Remove(mi);
//这里不起作用
lcdMenu.Items.Refresh();
menuItems.Remove(header);
}
return this;
}
public LcdMenuManager()
{
// TODO: Complete member initialization
}
public LcdMenuManager(Menu MainMenu)
{
// TODO: Complete member initialization
this.lcdMenu = MainMenu;
}