Focus/Command question

8 posts, 1 answers
  1. Thorsten
    Thorsten avatar
    26 posts
    Member since:
    Apr 2010

    Posted 31 Oct 2010 Link to this post

    Hi,

    shouldn't the RadContextMenu ensure on opening that the focus is within the attached element (or a child of it) in order to ensure that routed commands are working properly ?
    Lets say I've two controls A and B.
    Control B has a RadContextMenu set and contains a listbox.
    The context menu contains one item "Copy" with the corresponding ApplicationCommands.Copy command set.
    Control B has class command handlers for the Execute and CanExecute events of the ApplicationCommands.Copy command set.

    Now if  control A receives the focus, you can still open the context menu for control B by simply right-clicking on it.
    But the "Copy" menu item is now always disabled, since without the focus, the CanExecute and Execute event handlers of Control B are never called, so nobody listens on CanExecute in this case => the command is disabled.

    If you left click on B's listbox and then open the context menu via right click, everything works properly, since now the listbox or it's item has the focus and CanExecute event walks up to control b.

    Did I overlook something or do I really need to handle the RadContextMenu.ContextMenuOpening event manually and check whether to set the focus or not ?

    Thanks in advance

    Thorsten Klingert

  2. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 04 Nov 2010 Link to this post

    Hello Thorsten Klingert,

    It is not correct to always focus the control on right click. This is true even for left click. That is why we don't focus the control on right click.
    The default behavior of RoutedCommands is a little bit strange (to get the Focused element as command target) but you can override to when you set RadMenuItem.CommandTarget property. This way there will be no resolving (based on focused element) and everything should be working as expected.
    RadContextMenu have a small limitation - you cannot attach one instance to multiple elements. If you do so it will be attached only on the last element. One workaround for WPF is to define it as a resource and mark is as x:Shared=False. Then when you reference it you will get new instance.

    I hope that this information will be helpful.
    Let us know if you need further info.

    Kind regards,
    Hristo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Thorsten
    Thorsten avatar
    26 posts
    Member since:
    Apr 2010

    Posted 04 Nov 2010 Link to this post

    Thanks for the reply.

    Setting the CommandTarget property might by a bit tricky in my case since the menu items of an item are bound via the view model which
    isn't aware of the actual view.
    But I think a RelativeSource binding within the MenuItemContainerStyle should work.
    Anyway, I'll try it out, thanks.


  5. Franziska
    Franziska avatar
    29 posts
    Member since:
    Dec 2009

    Posted 25 Oct 2011 Link to this post

    Hello,

    Just found this thread which is actually the same problem that I have now.

    I have added a RadContextMenu to my GridView and now I want to fire the same commands as with the keyboard.
    For example, an Add menu item adds an item to the grid, a Delete item deletes the selected item, copy, paste and so on.

    First of all, the grid always looses its focus when the ContextMenu is opened. Is there a way to avoid that?
    I have a trigger which changes the border color if a GridView has IsKeyboardFocusWithin true, to visualize the user which GridView is selected. (Btw. I am wondering why there is no FocusWithin property on the GridView).

    My second problem is that the commands are not available in the context menu. I have found in some threads, that I have to set the CommandTarget, which I do as shown in my example.

    <telerik:RadContextMenu.ContextMenu>
                    <telerik:RadContextMenu ItemClick="RadContextMenu_ItemClick" Opened="RadContextMenu_Opened" >
                        <telerik:RadMenuItem Header="Add" Command="New" CommandTarget="{Binding RelativeSource={RelativeSource FindAncestor,
                            AncestorType={x:Type WPFTelerik:GridView}}}" />
                        <telerik:RadMenuItem Header="Delete" Command="Delete" />
                        <telerik:RadMenuItem Header="Copy" Command="Copy" />
                        <telerik:RadMenuItem Header="Paste" Command="Paste" />
                    </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>

     But this does not work! 
    What am I doing wrong? 

    Thanks,
    Franziska

  6. Rosi
    Admin
    Rosi avatar
    2527 posts

    Posted 26 Oct 2011 Link to this post

    Hello Franziska,

    For the first problem I suggest you set the Focusable property of contextmenu and of RadMenuItems to False.

    For the second the problem I suggest you edit the CommandTarget binding as follows:

    <telerik:RadMenuItem Header="{Binding CountryHeader}" Command="New" CommandParameter="{Binding Menu.UIElement, RelativeSource={RelativeSource Self}}" />

    Hope this helps.

    Kind regards,
    Rosi
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. Franziska
    Franziska avatar
    29 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2011 Link to this post

    Dear Rosi,

    Thanks for your response.

    You solved my first problem :)

    But the second one still exists. 
    Did you really mean to set the CommandParameter instead of the CommandTarget?
    Anyway, I tried it with both and it does not work. Even if I set the CommandParameter or CommandTarget explicitly in code-behind, it does not work. My the command is still disabled. 

    I also checked the CommandBinding and InputBinding collection of the GridView and both are empty. Shouldn't there be at least the standard bindings for New, Delete and so on?

    Maybe I am just binding to the wrong commands? What command do I have to bind to, to achive the same behavior as with the INSERT and DELETE key on the keyboard?

    Thanks,
    Franziska
  8. Franziska
    Franziska avatar
    29 posts
    Member since:
    Dec 2009

    Posted 26 Oct 2011 Link to this post

    Just answered my own question.

    For others having the same problem: RadGridViewCommands provides all commands of the GridView.
  9. Rosi
    Admin
    Rosi avatar
    2527 posts

    Posted 26 Oct 2011 Link to this post

    Hello Franziska,

    We are glad that you resolved the issue yourself.

    Do not hesitate to contact us again if you have any other questions related to our controls.

    Regards,
    Rosi
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
UI for WPF is Visual Studio 2017 Ready