C#winform使用XML绑定toolStripMenuItem生成菜单
开发环境:VS2008.NET2.0,网上找到的一个文章:Vs2005的win form动态生成菜单
找了很久才找到这个两年前的文章,是通过定义Dataset实现,既然.NET有内置的XML对象,就另写了一个,
另一个原因是,文中给出的XML文件,每条MenuItem都是由平行的MenuItemID和ParentItemID组成的,因为菜单是层次结构,而XML正好符合这个特性,
本例只涉及两层菜单:
private void CreateInstructionsListMenu(ToolStripMenuItem toolStripMenuItem_Instruments)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(@"..\..\Data\XMLInstructions.xml");
XmlNodeList nodeList = objXmlDoc.SelectSingleNode("InstructionList").ChildNodes;
foreach (XmlNode InstructionType in nodeList)
{
ToolStripMenuItem TypeMenu = new ToolStripMenuItem();
TypeMenu.Text = InstructionType.Attributes["CNName"].Value;
foreach (XmlNode Instruction in InstructionType.ChildNodes)
{
ToolStripMenuItem InstructionMenu = new ToolStripMenuItem();
InstructionMenu.Text = Instruction.Attributes["Name"].Value;
InstructionMenu.ShortcutKeyDisplayString = Instruction.Attributes["GMName"].Value;
InstructionMenu.Tag = Instruction.Attributes["GMId"].Value;
InstructionMenu.Click += new EventHandler(InstructionMenu_Click);
TypeMenu.DropDownItems.Add(InstructionMenu);
}
toolStripMenuItem_Instruments.DropDownItems.Add(TypeMenu);
}
}
相应的XML文件
<?xml version= "1.0" encoding= "utf-8" ?>
<InstructionList>
<Type CNName="钢琴" GMName="&Piano" AltKey="P">
<Instruction GMId="0" AltKey="A" GMName="&Acoustic Grand Piano" Name="大钢琴(声学钢琴)" />
<Instruction GMId="1" AltKey="B" GMName="&Bright Acoustic Piano" Name="明亮的钢琴"/>
<Instruction GMId="2" AltKey="E" GMName="&Electric Grand Piano" Name="电钢琴"/>
<Instruction GMId="3" AltKey="O" GMName="H&onky-tonk Piano" Name="酒吧钢琴"/>
<Instruction GMId="4" AltKey="R" GMName="&Rhodes Piano" Name="柔和的电钢琴"/>
<Instruction GMId="5" AltKey="C" GMName="&Chorused Piano" Name="加合唱效果的电钢琴"/>
<Instruction GMId="6" AltKey="H" GMName="&Harpsichord" Name="羽管键琴(拨弦古钢琴)"/>
<Instruction GMId="7" AltKey="L" GMName="C&lavichord" Name="科拉维科特琴(击弦古钢琴)"/>
</Type>
<Type CNName="色彩打击乐器" GMName="&Chromatic Percussion" AltKey="C">
<Instruction GMId="8" AltKey="C" GMName="&Celesta" Name="钢片琴"/>
<Instruction GMId="9" AltKey="G" GMName="&Glockenspiel" Name="钟琴"/>
<Instruction GMId="10" AltKey="B" GMName="Music &box" Name="八音盒"/>
<Instruction GMId="11" AltKey="V" GMName="&Vibraphone" Name="颤音琴"/>
<Instruction GMId="12" AltKey="M" GMName="&Marimba" Name="马林巴"/>
<Instruction GMId="13" AltKey="X" GMName="&Xylophone" Name="木琴"/>
<Instruction GMId="14" AltKey="T" GMName="&Tubular Bells" Name="管钟"/>
<Instruction GMId="15" AltKey="D" GMName="&Dulcimer" Name="大扬琴"/>
</Type>
<Type CNName="风琴" GMName="&Organ" AltKey="O">
<Instruction GMId="16" AltKey="O" GMName="Hammond &Organ" Name="击杆风琴"/>
<Instruction GMId="17" AltKey="P" GMName="&Percussive Organ" Name="打击式风琴"/>
<Instruction GMId="18" AltKey="R" GMName="&Rock Organ" Name="摇滚风琴"/>
<Instruction GMId="19" AltKey="C" GMName="&Church Organ" Name="教堂风琴"/>
<Instruction GMId="20" AltKey="E" GMName="R&eed Organ" Name="簧管风琴"/>
<Instruction GMId="21" AltKey="A" GMName="&Accordian" Name="手风琴"/>
<Instruction GMId="22" AltKey="H" GMName="&Harmonica" Name="口琴"/>
<Instruction GMId="23" AltKey="T" GMName="&Tango Accordian" Name="探戈手风琴"/>
</Type>
<InstructionList>
效果截图:
希望能对您有所帮助,
祝编程愉快!