I have a WPF application with a "ContentControl" in the main window and that has a treeview, menu and a data grid. They are populated when I do File-Open.
I've spent about a day trying to get keybindings working. The bindings are in a user control but I've read (http://blogs.southworks.net/geoff/2011/03/15/wpfshortcutkeys/) that you can get into problems unless you put it in the main window (this is a simple app I have).
So today I put a test command in the main window. It's a bit better - the shortcut works when I start the app up but after using File-Open (or other actions), it stops. I've used "Snoop" and can see that the focus is not set to any element - it's "null" when it's not working but also "null" at start-up when it does work.
The "Snoop" shows nothing in the events tab when I hit the shortcut keys. If I click on a control so that something has focus, the events appear again.
It's as if something gets focus that shouldn't.
I have a bit of text book XAML in the main window -
<Window ... Height="400" Width="772" Focusable="False" IsTabStop="False">
<Window.Resources>
<DataTemplate DataType="{x:Type vm:MainWindowContentViewModel}">
<local:MainWindowContent/>
</DataTemplate>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="Close"
CanExecute="CloseCanExecute"
Executed="CloseExecuted" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="A"
Modifiers="Control"
Command="Close" />
</Window.InputBindings>
<ContentControl x:Name="_contentControl"/>
</Window>
The code behind is also very boring (got it off the internet somewhere!).
private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Hey, I'm some help.");
e.Handled = true;
}
There's another complication in that most of my commands use the ICommand stuff but I've added this simple Help command to try and simplify things as it has the same issue with focus. (I use code from Keybinding a RelayCommand to get round that!)
Any hints?
Thanks.
Peter