MVVM Dynamic Menu UI from binding with ViewModel

前端 未结 5 1241
南方客
南方客 2020-11-29 00:53

I am working with a team on LoB application. We would like to have a dynamic Menu control, which creates the menu based on the logged in user profile. In previo

5条回答
  •  爱一瞬间的悲伤
    2020-11-29 01:26

    This solution doesn't need any code in code behind and that makes it simpler solution.

            
                
                    
                        
                            
                        
                        
                            
                                
                                    
                                        
                                    
                                
                            
                        
                    
                
            
    

    And MenuItem is represented as:

    public class MenuItemViewModel : BaseViewModel
        {
            /// 
            /// Initializes a new instance of the  class.
            /// 
            /// The parent view model.
            public MenuItemViewModel(MenuItemViewModel parentViewModel)
            {
                ParentViewModel = parentViewModel;
                _childMenuItems = new ObservableCollection();
            }
    
            private ObservableCollection _childMenuItems;
            /// 
            /// Gets the child menu items.
            /// 
            /// The child menu items.
            public ObservableCollection ChildMenuItems
            {
                get
                {
                    return _childMenuItems;
                }
            }
    
            private string _header;
            /// 
            /// Gets or sets the header.
            /// 
            /// The header.
            public string Header
            {
                get
                {
                    return _header;
                }
                set
                {
                    _header = value; NotifyOnPropertyChanged("Header");
                }
            }
    
            /// 
            /// Gets or sets the parent view model.
            /// 
            /// The parent view model.
            public MenuItemViewModel ParentViewModel { get; set; }
    
            public virtual void LoadChildMenuItems()
            {
    
            }
        }
    

    The concrete MenuItems can be either instantiated directly or you could make your own SubTypes through inheritance.

提交回复
热议问题