Could the data be loaded from an XML

Dec 4, 2010 at 5:38 PM

Hy,

How can i load the item of the menu from a XML file.
Is this posible?

Thanks.

Coordinator
Dec 6, 2010 at 12:03 PM
Edited Dec 6, 2010 at 12:10 PM

Sorry but this will only be supported in future versions if time permits. However you don't have it to happen. With a bit of coding you should be able to get this to work just fine.

For instance you can use the AddMenu function to add new menus to your PopupMenu while iterating though each XML element. Here is a parsing sample to get you started:

private IEnumerable<PopupMenuItem> ParseData(Stream xmlStream)
{
 XmlReader reader = XmlReader.Create(xmlStream);
 XDocument xml = XDocument.Load(reader);
 var items = from node
 in xml.Descendants("Item")
    where node.Parent == xml.Root
    select
 new PopupMenuItem
    {
     Header = (
string)node.Attribute("Header").Value,
     ItemsSource = GetDescendants(node)
    };
 
return items as IEnumerable<PopupMenuItem>;
}
// Recursive method that is used if an item has sub-items
private IEnumerable<PopupMenuItem> GetDescendants(XElement node)
{
 
return  from item in node.Descendants("Header")
  where item.Parent.Attribute(
"Header").Value == node.Attribute("Header").Value
  select
 new PopupMenuItem
  {
   Header = (
string)item.Attribute("Header").Value,
   ItemsSource = GetDescendants(item)
  };
}

And here is the XML sample you would use with the code:

<?xml version="1.0" encoding="utf-8"?>
<
Menu>
<Item Header="File">
 
<Item Header="New" />
 
<Item Header="Open">
  
<Item Header="Project">
   
<Item Header="Local Project" />
   
<Item Header="Project From Web" />
  
</Item>
  
<Item Header="File">
   
<Item Header="Local File" />
   
<Item Header="File from Web" />
  
</Item>
 
</Item>
 
<Item Header="Close" />
 
<Item Header="Add New Item" />
 
<Item Header="Add Existing Item" />
 
<Item Header="Add Project" />
</Item>
<Item Header="Edit">
 
<Item Header="Undo" />
 
<Item Header="Redo" />
 
<Item Header="Cut" />
 
<Item Header="Copy" />
 
<Item Header="Paste" />
</Item>
</
Menu>