I’m exploring WPF and XAML for the first time, and there’s a lot I don’t yet understand.
I’m working through this: http://www.codeproject.com/KB/WPF/AvalonDockMVVM.aspx
I’ve created my own project, and am attempting to add to it code from AvalonDockMVVM.
Currently, I’m trying to construct the menu.
In MainWindow.xaml, I’ve added:
<Window.Resources>
<RoutedUICommand x:Key="Commands.Exit" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource Commands.Exit}" Executed="Exit_Executed" />
</Window.CommandBindings>
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Exit" Command="{StaticResource Commands.Exit}" />
</MenuItem>
</Menu>
...
</DockPanel>
The idea is to construct a menu with a single “Exit” option.
Now as I said, I hardly know what I am doing. But it looks to me like the <RoutedUICommand> is creating a command, the <CommandBinding> is binding it to the ExitExecuted() method in the code-behind, and the <MenuItem> is saying that when you select that menu item, execute whatever is bound to that command.
It all works fine, in the AvalonDockMVVM project. In mine, I’m betting errors on {StaticResource Commands.Exit}: The resource Commands.Exit could not be resolved.
That makes me think that there’s something, in code or in configuration, somewhere, that defines Commands.Exit, besides the <RoutedUICommand> element in the XAML. But for the life of me, I can’t find it.
Is there something else I need to do, in order to bind a method to a menu, through a command?
I literally copied your above example into a new WPF project, merely removing the ellipses and adding a method in code behind to handle the exit command. It ran just fine and had no problem resolving the command. Here’s my entire, and complete, XAML
There’s two places I’d look for trouble resolving the resource. First, check to see if you have a project-level static resource defined with a “Commands.Exit” key on it (or another resource further down the page). Second, look to see if code above the <RoutedUICommand> or <Window.Resources> has an un-closed tag. That should throw up a parse error, but maybe not?