WPF - How can I create menu and submenus using binding

前端 未结 3 2063
鱼传尺愫
鱼传尺愫 2020-11-28 06:30

I am trying to create a dynamic menu using binding. I my viewmodel I have a list of objects which contains an header and a command. However, it is not working. I think the p

3条回答
  •  孤城傲影
    2020-11-28 07:21

    For me, it worked with this simple template:

    
        
    
    
        
            
        
    
    

    Here is the complete example:

    MainWindow.xaml:

    
        
            
                
                    
                
                
                    
                        
                    
                
            
            
            
        
    
    

    MainWindow.xaml.cs:

    using System;
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Input;
    
    namespace WpfApplication14
    {
        public partial class MainWindow : Window
        {
            public ObservableCollection MenuItems { get; set; }
    
            public MainWindow()
            {
                InitializeComponent();
    
                MenuItems = new ObservableCollection
                {
                    new MenuItemViewModel { Header = "Alpha" },
                    new MenuItemViewModel { Header = "Beta",
                        MenuItems = new ObservableCollection
                            {
                                new MenuItemViewModel { Header = "Beta1" },
                                new MenuItemViewModel { Header = "Beta2",
                                    MenuItems = new ObservableCollection
                                    {
                                        new MenuItemViewModel { Header = "Beta1a" },
                                        new MenuItemViewModel { Header = "Beta1b" },
                                        new MenuItemViewModel { Header = "Beta1c" }
                                    }
                                },
                                new MenuItemViewModel { Header = "Beta3" }
                            }
                    },
                    new MenuItemViewModel { Header = "Gamma" }
                };
    
                DataContext = this;
            }
        }
    
        public class MenuItemViewModel
        {
            private readonly ICommand _command;
    
            public MenuItemViewModel()
            {
                _command = new CommandViewModel(Execute);
            }
    
            public string Header { get; set; }
    
            public ObservableCollection MenuItems { get; set; }
    
            public ICommand Command
            {
                get
                {
                    return _command;
                }
            }
    
            private void Execute()
            {
                // (NOTE: In a view model, you normally should not use MessageBox.Show()).
                MessageBox.Show("Clicked at " + Header);
            }
        }
    
        public class CommandViewModel : ICommand
        {
            private readonly Action _action;
    
            public CommandViewModel(Action action)
            {
                _action = action;
            }
    
            public void Execute(object o)
            {
                _action();
            }
    
            public bool CanExecute(object o)
            {
                return true;
            }
    
            public event EventHandler CanExecuteChanged
            {
                add { }
                remove { }
            }
        }
    }
    

    The resulting window looks like this:

    screen shot

提交回复
热议问题