I am using XamMenu for our primary application menu. Rather than hard code or add all the menus on the fly I want to use data binding. Our application menus are currently defined in XML. I can use linq to put them in an object graph if needed.
Reading the docs it talks about creating a hierarchal template but seems to say you need to create one for each menu level you need. Our menu structure is 100% defined in the XML.
First, is it possible to bind to XML or will I need to bind to an object graph.
Are there any examples of fully data driven XamMenus with random level depths.
BOb
Can anyone help me binding the XamMenu to an ObservableCollection in a ViewModel. I'm not quite sure how to set up the HierarchyTemplate object. I am just getting empty menus but I know the observable collection is getting populated.
Bob,
The XamDataTree will not be the best option. We may be able to get this to work now with the XamMenu with its current features. Please attach a sample XML file that you might use build a menu. I think we may be able to employ LINQ To XML to get this working.
Thanks,
Francis,
I have implemented INotifyPropertyChanged in the view model. I have not in the MenuItem class since the menu items don't change.. just the collection (IList). I think I tried it both as an ObservableCollection and as the EntityCollection (that comes back from RIA.)
I have worked out the linq to XML to create the object graph. The code runs in my DomainService which returns the menu data as a List. Here is the code of our Menu service that creates the object graph from our menu xml:
namespace AmsiWeb.ConfigServices
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Xml.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Collections;
using System.Text.RegularExpressions;
using System.Xml;
[EnableClientAccess()]
public class MenuService : DomainService
int _menuIdIterator;
public IEnumerable<MenuItem> LoadTocBranch(string tocPath, string menuProvider)
XDocument xdoc = XDocument.Load(new XmlNodeReader(MenuHelper.GetMenu(tocPath, menuProvider)));
var menu = (from node in xdoc.Root.Elements("NODE")
select new MenuItem
Id = ++_menuIdIterator,
MenuTitle = (string)node.Attribute("text"),
NavigationUri = ParseUri((string)node.Attribute("navigate")),
IconPath = (string)node.Attribute("iconPath"),
NodeSource = (string)node.Attribute("nodeSrc"),
NodeProvider = (string)node.Attribute("nodeProvider"),
Children = GetNodeChildren(node.Elements("NODE"), node)
}).ToList<MenuItem>();
return menu;
}
private IList<MenuItem> GetNodeChildren(IEnumerable<XElement> elements, XElement parentNode)
var parentId = _menuIdIterator;
return (from node in elements
ParentMenuId = parentId,
/// <summary>
/// Pre-pend Html/ to any Uri that ends with .htm or .html or .aspx. This will facilitate the UriMapper and navigation in the silverlight client.
/// </summary>
/// <param name="Uri">The Uri to parse.</param>
/// <returns></returns>
private string ParseUri(string Uri)
if ((! String.IsNullOrEmpty(Uri)) && Regex.IsMatch(Uri, @"(.*?)\.(html|htm|aspx|asp)"))
return "Html/" + System.Web.HttpUtility.UrlEncode(Uri);
return Uri;
public class MenuItem
[Key]
public int Id { get; set; }
public string MenuTitle { get; set; }
public string NavigationUri { get; set; }
public string IconPath { get; set; }
public string NodeProvider { get; set; }
public string NodeSource { get; set; }
public int ParentMenuId { get; set; }
[Include]
[Composition]
[Association("Children","Id","ParentMenuId")]
public IList<MenuItem> Children { get; set; }
public MenuItem()
Children = new List<MenuItem>();
If I have time I will try to isolate this all into a single sample with what I was trying to do. I really need to get this working. Thanks.
I'm sorry for not getting back to you. Have you made any progress since with a sample since 12/29, or do you need us to try to create one? I will keep my eye on this thread for your reply.
I've been pretty busy... I'm not sure how a sample of something that isn't working is going to help. ;)
I took a slight step back... Attached is a sample demonstrating a basic approach to displaying Hierarchical data in the XamMenu. The data is generated in code. Please review this and let me know if this approach works with your data or if there is a specific problem we can focus on.
Just following up on the sample I sent on Jan 24. Did you have any specific questions about that sample that I can assist with?
The same XMAL worked to load the Menu items. but how this will work with the command.
I tried this way
<i:Interaction.Triggers> <i:EventTrigger EventName="ItemClicked"> <i:InvokeCommandAction Command="{Binding Path=SwitchPageCommand}" CommandParameter="{Binding Path=MenuName}" /> </i:EventTrigger> </i:Interaction.Triggers>
AND
<ig:XamMenuItem.InputBindings> <MouseBinding MouseAction="LeftClick" Command="{Binding Path=SwitchPageCommand}" CommandParameter="{Binding Path=MenuName}" /> </ig:XamMenuItem.InputBindings>
both the way but its not firing the event and not giving the parameter values. Can you help me on it
One of the developers informed me that we were neglecting to use the DefaultItemsContainer to host our DataTemplate for the XamMenuItem. An example is posted in our forums in a thread entitled "Horizontal XamMenu Data binding with Hierarchical Data" at this address: https://ko.infragistics.com/community/forums/f/retired-products-and-controls/49840/horizontal-xammenu-data-binding-with-hierarchicaldata/264957#264957
I believe the proper markup in my own sample should be the following...
<ig:XamMenu Name="xamMenu1" ItemsSource="{Binding MenuItems }"> <ig:XamMenu.HierarchicalItemTemplate > <ig:HierarchicalDataTemplate ItemsSource="{Binding Children }"> <ig:HierarchicalDataTemplate.DefaultItemsContainer > <DataTemplate > <ig:XamMenuItem /> </DataTemplate > </ig:HierarchicalDataTemplate.DefaultItemsContainer > <ig:HierarchicalDataTemplate.ItemTemplate > <DataTemplate > <TextBlock Text="{Binding MenuTitle }"/> </DataTemplate > </ig:HierarchicalDataTemplate.ItemTemplate > <DataTemplate > <TextBlock Text="{Binding MenuTitle }"/> </DataTemplate > </ig:HierarchicalDataTemplate > </ig:XamMenu.HierarchicalItemTemplate > </ig:XamMenu >
Just wanted to let you know that this topic is being discussed between myself and my colleagues. I will let you know our findings as soon as I can.
Thanks Bob....I'll look into this and get back to you shortly.
Ok a question.. all I get is text blocks in the menu. Shouldn't they be XamMenu items. I need to set the Header and NavigationUri along with a right click event handler. The "menu" displays but it doesn't do anything.