0

I tried this:

<Page.InputBindings>
    <KeyBinding Command="{Binding NavigationCommands.BrowseBack}" Key="Esc"/>
</Page.InputBindings>

And this:

<Page.InputBindings>
    <KeyBinding Command="NavigationCommands.BrowseBack" Key="Esc"/>
</Page.InputBindings>

But still the navigation doesn't go back when I press escape. How do I make this key binding work?

ekolis
  • 6,270
  • 12
  • 50
  • 101

1 Answers1

1

It is most probably because the page is not in focus. I guess you load the page in a frame. To check if it is generally working, click anywhere in the page and hit esc.

Possible solutions i know are: 1) use focus manager on load of page to set focus in the page (and not have focus on the window)

2) set the binding not on the page but on the window. This can be tricky architectur-wise. Because if you load different pages you have to load and unload that event if is page dependent.

Hope that helps.

Jan
  • 3,825
  • 3
  • 31
  • 51
  • How do I use FocusManager to set focus to the page? – ekolis Sep 29 '18 at 16:51
  • by setting the focused element to any element in that page. It may be possible to set it to the page itself. see here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.focusmanager.focusedelement?view=netframework-4.7.2 and here the first two answers: https://stackoverflow.com/questions/11246554/how-to-set-autofocus-only-in-xaml – Jan Sep 29 '18 at 19:04
  • Nope, I tried `FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"` in the and that didn't help... – ekolis Oct 02 '18 at 01:19
  • try it on a different element i.e. a textbox. – Jan Oct 02 '18 at 04:46
  • If I click a button then the key binding works; how would I bind it to a button? I tried `{Binding RelativeSource={RelativeSource Self}, Path=btnShipStats}` and `{Binding Path=btnShipStats}` but neither of those worked; I have to manually click the button to focus it. – ekolis Oct 06 '18 at 18:09
  • Oh, I just put `btnShipStats.Focus();` in the page constructor and that worked! – ekolis Oct 06 '18 at 18:10
  • if you use relative source=self, then you put it directly on the button in xaml – Jan Oct 06 '18 at 18:11