3

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

Community
  • 1
  • 1

2 Answers2

1

I would set IsTabstop=false on the Contentcontrol - it is default true and you can't see when it has focus because it doesn't have a style/controltemplate.

It looks like the contentcontrol has focus when your program starts...

Also I would remove Focusable=false on the window - it could be a cause of focus-limbo.

Rune Andersen
  • 1,650
  • 12
  • 15
0

Can't seem to reply in-line to the questions - perhaps because I posted before I signed in!

@Rune Andersen

Tried that - didn't help. Couldn't see any difference.

Does setting Focus manually work ?

I can set it manually once at start up then the Ctrl-O works first time. Then it doesn't work after the open file dialogue.

did it work after doing this.Focus() in Window.xaml.cs after you Open the File Dialog?

I have not tried that but I don't really want to have to do this after every situation where it loses focus - I've not identified all of them. Perhaps that's what I'll have to do - seems a bit poor.

Thanks.

Peter.

Peter S
  • 191
  • 1
  • 2
  • 10