Command Binding in hierarchical datatemplate

前端 未结 3 502
遇见更好的自我
遇见更好的自我 2021-02-06 09:45

I have Menu in my app. I\'m visualizing it using hierarchical data template:

             


        
3条回答
  •  鱼传尺愫
    2021-02-06 10:03

    Seems like I've found solution for part of my problem. Command is not being binding because, it seems like we need to create particular instance of command for each menu item. The core problem is that, allmost all my menuitems execute the same command and differences are only in value of command parameter. So I should do so:

    sample menuitem class:

    public class RMyMenuItem
    {
        public string Name { get; set; }
    
        public string InputGesture { get; set; }
    
        public ICommand ItemCommand
        { get; set; }
    
        public List ChildrenItems { get; set; }
    }
    

    property in ViewModel:

    public ObservableCollection ApplicationMenu
    {
        get
        {
            //RApplicationMainMenu menu = new RApplicationMainMenu(0);
            //return new ObservableCollection(menu.Items);
            return new ObservableCollection()
            {
            new RMyMenuItem()
                {
                    Name = "item1",                    
                    ItemCommand = new DelegateCommand((param) => RunOperationExecute(param)),
                    ChildrenItems = new List()
                    {
            new RMyMenuItem()
            {
                Name = "item2",
                ItemCommand = new DelegateCommand((param) => RunOperationExecute(param))
            }
                    }
                }
        };
        }
    

    And XAML:

        
            
        
    }
    

提交回复
热议问题