locked
Problem with KeyBinding and focus... RRS feed

  • Question

  • I'm using KeyBindings in my App to enable hotkeys. I have several that work fine, and then a couple that only work sometimes.

    In my happen a user can click on a schedule control and I create a new schedule item and give it focus so that if they hit the Delete key, then I can delete it. So far no problem, everything works fine. Another feature of my app is that you can create multiple "tabs" like in FireFox or IE and navigate between the different documents (I accomplish this uses a Frame control with Pages). To create a new tab, I have a KeyBinding configured to accept "Ctrl+N". This also works fine. To navigate between the pages, I created a KeyBinding for "Ctrl+Left" and "Ctrl+Right". These also work fine, except when I give one of my schedule items Keyboard focus. "Ctrl+N", however, continues to work with or without focus being on the schedule item.

    Here are my KeyBindings. Any thoughts?

    Code Snippet


    <Window.InputBindings>
            <KeyBinding Key="N" Modifiers="Control" Command="local:ApplicationWindow.NewDocument"/>
            <KeyBinding Key="Left" Modifiers="Control" Command="local:ApplicationWindow.GoBack"/>
            <KeyBinding Key="BrowserBack" Modifiers="Control" Command="local:ApplicationWindow.GoBack"/>
            <KeyBinding Key="Right" Modifiers="Control" Command="local:ApplicationWindow.GoForward"/>
            <KeyBinding Key="BrowserForward" Modifiers="Control" Command="local:ApplicationWindow.GoForward"/>
        </Window.InputBindings>



    Thanks,
    --Jeremy

    Wednesday, March 19, 2008 1:04 AM

Answers

  • Is it possible that CanExecute for your GoBack and GoForward commands is returning false while the other user control has focus?

     

    It sounds like the command binding is on your window.  As a test, can you add a CommandTarget to the KeyBinding to explicitly target the window?

     

    Code Snippet

     

    <KeyBinding Key="Left" Modifiers="Control"
        Command="local:ApplicationWindow.GoBack"

        CommandTarget="{x:Static local:ApplicationWindow.Instance}" />

     

     

     

    This will require that you expose a property called Instance on your window, like so:

     

    Code Snippet

     

    public static ApplicationWindow Instance

    {

        get { return Application.Current.MainWindow; }

    }

     

     

     

    (Yes, I'm making an assumption that your ApplicationWindow object is basically a singleton and the main window.)
    Wednesday, March 19, 2008 6:51 AM

All replies

  • What is your schedule control?  Is it an items control?  What is its base class?

     

    What is a schedule item?  Is it the container for an items control?

     

    Something else is likely handling the Ctrl+Left/Ctrl+Right input events.  The solution will likely involve determining what that is and adding a KeyBinding for your commands between it and the focused element. 

     

    I would start be placing a KeyBinding directly on the focused element and invoking your command to change tabs there.  If that doesn't work, then the focused element may be handling the input event and filtering key strokes in a class handler.  (Note that UIElement registers a ton of class handlers for input events and exposes them to derived classes in the form of virtual methods like OnPreviewKeyDown, OnKeyDown, OnPreviewKeyUp, OnKeyUp, etc....

     

     

     

    Wednesday, March 19, 2008 3:49 AM
  • My schedule control is a UserControl that hosts a Canvas. I custom draw the grid lines and time labels for the control (the granularity of the control could require up to 2.5 million items (it is a video app, and the granularity of the schedule control is down to the frame level)), so the performance impact of creating that many border controls was obviously a major problem. With the Canvas I just render the portion of the Schedule that is scrolled into view. The schedule items are also a user control that host a Grid, with a couple of TextBlocks.

    I also suspected that something else might be eating the events, so I used Snoop to view the event chain, and Snoop indicates that the event perculates all the way to the top level window without ever being handled. When focus is not on the schedule item, though, then the event gets to the top level window where my handler is called, and Snoop shows that the handled property gets set to true.



    --Jeremy

    Wednesday, March 19, 2008 4:35 AM
  • Is it possible that CanExecute for your GoBack and GoForward commands is returning false while the other user control has focus?

     

    It sounds like the command binding is on your window.  As a test, can you add a CommandTarget to the KeyBinding to explicitly target the window?

     

    Code Snippet

     

    <KeyBinding Key="Left" Modifiers="Control"
        Command="local:ApplicationWindow.GoBack"

        CommandTarget="{x:Static local:ApplicationWindow.Instance}" />

     

     

     

    This will require that you expose a property called Instance on your window, like so:

     

    Code Snippet

     

    public static ApplicationWindow Instance

    {

        get { return Application.Current.MainWindow; }

    }

     

     

     

    (Yes, I'm making an assumption that your ApplicationWindow object is basically a singleton and the main window.)
    Wednesday, March 19, 2008 6:51 AM
  • Ironically, I already had a static Instance method. Smile

    Unfortuneately, when I added the CommandTarget to my XAML, I get the following exception in the InitializeComponent call in my ApplicationWindow:


    "A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

    Additional information: Cannot convert the value in attribute 'CommandTarget' to object of type 'System.Windows.Markup.StaticExtension'.  Error at object 'System.Windows.Input.KeyBinding' in markup file 'Pipeline;component/applicationwindow.xaml' Line 23 Position 89."
    Wednesday, March 19, 2008 3:53 PM
  • Did you find out what the problem was? I have similar problems with my commands.
    Wednesday, February 24, 2010 4:09 PM