WPF - Bind Menu Item

Posted by Miguel on Stack Overflow See other posts from Stack Overflow or by Miguel
Published on 2009-11-20T02:33:43Z Indexed on 2010/06/08 2:02 UTC
Read the original article Hit count: 599

Filed under:
|
|
|

Hello,

I am creating a menu and binding the menu items ate runtime as follows but I am not able to make it work.

I am creating the menu as follows:

  Menu menu = new Menu();      
  menu.Items.Add(new MenuItem {  Command = new PackCommand(), Header = "Pack" });
  DockPanel.SetDock(menu, Dock.Top);
  content.Children.Add(menu);

And I am implementing ICommand:

public static class PackCommand : ICommand {
  Boolean CanExecute(object parameter) {
    return true;
  }
  void Execute(object parameter) {
    Packer packer = new Packer();
    packer.Run();
  }
}
  1. I am not sure how to bind the menu item.
  2. Why CanExecute? Shouldn't it always? I only want to run packer.Run when the buttom is clicked.

I think I should implement ICommand but I am not even sure I should do this?

Could someone please help me out?

Thanks, Miguel

© Stack Overflow or respective owner

Related posts about wpf

Related posts about menu