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="&amp;Piano" AltKey="P">
    <Instruction GMId="0" AltKey="A" GMName="&amp;Acoustic Grand Piano" Name="大钢琴(声学钢琴)" />
    <Instruction GMId="1" AltKey="B" GMName="&amp;Bright Acoustic Piano" Name="明亮的钢琴"/>
    <Instruction GMId="2" AltKey="E" GMName="&amp;Electric Grand Piano" Name="电钢琴"/>
    <Instruction GMId="3" AltKey="O" GMName="H&amp;onky-tonk Piano" Name="酒吧钢琴"/>
    <Instruction GMId="4" AltKey="R" GMName="&amp;Rhodes Piano" Name="柔和的电钢琴"/>
    <Instruction GMId="5" AltKey="C" GMName="&amp;Chorused Piano" Name="加合唱效果的电钢琴"/>
    <Instruction GMId="6" AltKey="H" GMName="&amp;Harpsichord" Name="羽管键琴(拨弦古钢琴)"/>
    <Instruction GMId="7" AltKey="L" GMName="C&amp;lavichord" Name="科拉维科特琴(击弦古钢琴)"/>
  </Type>
  <Type CNName="色彩打击乐器" GMName="&amp;Chromatic Percussion" AltKey="C">
    <Instruction GMId="8" AltKey="C" GMName="&amp;Celesta" Name="钢片琴"/>
    <Instruction GMId="9" AltKey="G" GMName="&amp;Glockenspiel" Name="钟琴"/>
    <Instruction GMId="10" AltKey="B" GMName="Music &amp;box" Name="八音盒"/>
    <Instruction GMId="11" AltKey="V" GMName="&amp;Vibraphone" Name="颤音琴"/>
    <Instruction GMId="12" AltKey="M" GMName="&amp;Marimba" Name="马林巴"/>
    <Instruction GMId="13" AltKey="X" GMName="&amp;Xylophone" Name="木琴"/>
    <Instruction GMId="14" AltKey="T" GMName="&amp;Tubular Bells" Name="管钟"/>
    <Instruction GMId="15" AltKey="D" GMName="&amp;Dulcimer" Name="大扬琴"/>
  </Type>
  <Type CNName="风琴" GMName="&amp;Organ" AltKey="O">
    <Instruction GMId="16" AltKey="O" GMName="Hammond &amp;Organ" Name="击杆风琴"/>
    <Instruction GMId="17" AltKey="P" GMName="&amp;Percussive Organ" Name="打击式风琴"/>
    <Instruction GMId="18" AltKey="R" GMName="&amp;Rock Organ" Name="摇滚风琴"/>
    <Instruction GMId="19" AltKey="C" GMName="&amp;Church Organ" Name="教堂风琴"/>
    <Instruction GMId="20" AltKey="E" GMName="R&amp;eed Organ" Name="簧管风琴"/>
    <Instruction GMId="21" AltKey="A" GMName="&amp;Accordian" Name="手风琴"/>
    <Instruction GMId="22" AltKey="H" GMName="&amp;Harmonica" Name="口琴"/>
    <Instruction GMId="23" AltKey="T" GMName="&amp;Tango Accordian" Name="探戈手风琴"/>
  </Type>

<InstructionList>

效果截图:

 

 

希望能对您有所帮助,

祝编程愉快!

 

posted @ 2009-02-20 22:48  风海迷沙  阅读(5028)  评论(6编辑  收藏  举报