ContextMenu focus issue

Ribbon for WPF Forum

Posted 8 years ago by Kasper
Version: 11.2.0552
Platform: .NET 4.0
Environment: Windows 7 (32-bit)
Avatar
Hi guys,

I have a ListView with a Ribbon ContextMenu. When clicking on one of the buttons, I wish to add some text to a SyntaxEditor control and then give focus to this SyntaxEditor. After setting the text, I call se.Focus(). Unfortunately it seems like the focus remains on the ListView, perhaps because the context menu closes and then gives focus back to the ListView after I have tried giving the focus to the SyntaxEditor? If I replace the Ribbon ContextMenu with a regular WPF ContextMenu and run the exact same code, everything works as expected and my SyntaxEditor receives the focus.

Comments (5)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
There might be some code in ribbon:Button to blur focus back to the main control in the focus scope. You could probably do a Dispatcher.BeginInvoke of your focus code to work around it.


Actipro Software Support

Posted 8 years ago by Kasper
Avatar
Hi,

First of all, thank you for the workaround. However, I'm curious to know if you consider this a bug, which eventually will be fixed, or if this alternative approach to dealing with focus will be used in the future as well?

Secondly, I tried something like this, as per your suggestion:

this.Dispatcher.BeginInvoke((Action)(() =>
{
    if(SyntaxEditor.Focusable)
        SyntaxEditor.Focus();
}));
But it doesn't seem to make a difference. Am I not doing it right or...?
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kasper,

Can you please put together a new simple sample project that shows the issue and send it over to our support address? We'll debug that to see what is going on. Rename the .zip file extension so that it doesn't get spam blocked. Thanks!


Actipro Software Support

Posted 8 years ago by Kasper
Avatar

Hi,

As mentioned, it's extremely easy to reproduce, but I have sent you a sample project nevertheless. Let me know what you think :)

Answer - Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Kasper,

While we've narrowed down what is happening, I'm not sure of the best way to fix it from within our product. It seems that when the ContextMenu loses mouse capture, it resets the focus back to the source control that the context menu is for. We raise our button's click event similar to how standard WPF menu items do, so I'm not sure why in our case the mouse capture is changing after the click vs. before.

A workaround in this case is to do the following in your Button_Click:

var cm = ActiproSoftware.Windows.Media.VisualTreeHelperExtended.GetAncestor((UIElement)sender, typeof(ContextMenu)) as ContextMenu;
if (cm != null)
	cm.ReleaseMouseCapture();

txtTest.Focus();

Or if you have the code you sent but make your BeginInvoke use a DispatcherPriority.Input, it will work as well.

[Modified 8 years ago]


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.