Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
165
XamMenu ItemClicked on MVVM not giving command parameter Value on the function call
posted

<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.

Parents
  • 54937
    Offline posted

    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>

Reply Children