<UserControl.Resources>
<DataTemplate x:Key="MenuDataTemplate"> <StackPanel Orientation="Horizontal"> <Border Padding="0"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=MenuName}"/> </StackPanel> </Border> </StackPanel> </DataTemplate> </UserControl.Resources>
<ig:XamMenu x:Name="MainMenu" VerticalAlignment="Top" MenuOrientation="Horizontal" ItemTemplate="{StaticResource MenuDataTemplate}" ItemsSource="{Binding Path=userMenuItems}" HorizontalAlignment="Stretch" > <i:Interaction.Triggers> <i:EventTrigger EventName="ItemClicked"> <i:InvokeCommandAction Command="{Binding Path=SwitchPageCommand}" CommandParameter="{Binding Path=MenuName}" /> </i:EventTrigger> </i:Interaction.Triggers> <ig:XamMenu.HierarchicalItemTemplate> <ig:HierarchicalDataTemplate ItemTemplate="{StaticResource MenuDataTemplate}" ItemsSource="{Binding Path=Children}" > <ig:HierarchicalDataTemplate.DefaultItemsContainer> <DataTemplate> <ig:XamMenuItem> <ig:XamMenuItem.Icon> <Image Source="{Binding MenuImageURL}" /> </ig:XamMenuItem.Icon> </ig:XamMenuItem> </DataTemplate> </ig:HierarchicalDataTemplate.DefaultItemsContainer> </ig:HierarchicalDataTemplate> </ig:XamMenu.HierarchicalItemTemplate> </ig:XamMenu>
ViewModel Code :
public ICommand SwitchPageCommand { get; private set; }
SwitchPageCommand = new DelegateCommand(executeAction: param => MenuItemClick(param), canExecute: delegate { return true; });
public void MenuItemClick(object menuID) {
// function is called but not giving any parameter values }
Model Class:
[DataContract] public class MenuDataItem { [DataMember] public string MenuName { get; set; } [DataMember] public int? MenuId { get; set; } //[DataMember] //public int? ParentId //{ // get; // set; //} [DataMember] public string MenuImageURL { get; set; } [DataMember] public List<MenuDataItem> Children { get; set; } }
May I know what should we make a change to correct it.
I think the issue is that there are 2 bindings set on the InvokeCommandAction with the expectation/assumption that they get values from different objects. The DataContext of the action would be that of the Menu. The menu's datacontext will be the VM which has the SwitchPageCommand property but it doesn't have a MenuName property. That is a property of the dataitem for each item (i.e. the datacontext for each XamMenuItem). I think that you will need to handle this in the DefaultItemsContainer hooking the Click event (using the EventTrigger as you are). Something like:
<DataTemplate> <ig:XamMenuItem> <ig:XamMenuItem.Icon> <Image Source="{Binding MenuImageURL}" /> </ig:XamMenuItem.Icon> <i:Interaction.Triggers> <i:EventTrigger EventName="Click"> <i:InvokeCommandAction Command="{Binding Path=DataContext.SwitchPageCommand, RelativeSource={RelativeSource AncestorType={x:Type ig:XamMenu}}}" CommandParameter="{Binding Path=MenuName}" /> </i:EventTrigger> </i:Interaction.Triggers> </ig:XamMenuItem> </DataTemplate>
Thanks for your response. But the solution does not work. Its not even loading menu properly and not firing event also on Click Command.
I dont have any binding error also in my output window.
For better debuging purpose I am sending my project (sample) sending to you. Please take a look and make it working for you.
HI Paresh,
I was able to get past the unknown build error.
I ran your applicartion and you are getting a BindingExpression error
System.Windows.Data Error: 40 : BindingExpression path error: 'MenuName' property not found on 'object' ''RegionSetViewModel' (HashCode=28798327)'. BindingExpression:Path=MenuName; DataItem='RegionSetViewModel' (HashCode=28798327); target element is 'InvokeCommandAction' (HashCode=65768324); target property is 'CommandParameter' (type 'Object')
MenuName is not part of your RegionSetViewModel.
Sincerely, Matt Developer Support Engineer