RibbonButton in ContextMenu Focus problem

Ribbon for WPF Forum

Posted 10 years ago by miky
Version: 9.2.0515
Platform: .NET 4.0
Environment: Windows XP (32-bit)
Avatar
Hello,
I have a TreeView control, and i attached the TreeViewItem.ContextMenu to the following:

<ribbon:ContextMenu x:Key="contextDeviceExpander">           
    <ribbon:Button Label="New Folder" Command="local:TreeControl.NewFolderCommand" />
</ribbon:ContextMenu>
The NewFolderCommand creates a new TreeViewItem which have a TextBox inside it's template.

I immediately call the TextBox's Focus function, but right after the receiving the focus, it loses it.

This only happens if i use <ribbon:Button ../> (MenuItem works perfect)
I tried setting the Focusable of the button to False, but this didn't change anything.

Thanks in advance,
Miky.

Comments (3)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Miky,

We'd probably need to have you email us a simple sample project that shows this happening so we can debug it. If you send a ZIP over please don't include any .exe files in the ZIP.


Actipro Software Support

Posted 10 years ago by miky
Avatar
Hello,
Thank you for you very quick reply.
It's pretty easy to reproduce the bug. here's what it takes:

<Window x:Class="BugTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:ribbon="http://schemas.actiprosoftware.com/winfx/xaml/ribbon">
    <Window.Resources>
        <ribbon:ContextMenu x:Key="contextMenu">
            <ribbon:Button Label="SelectAll" Click="Button_Click" />
        </ribbon:ContextMenu>
        
    </Window.Resources>
    <Grid>
        <TextBox Width="100" Margin="0,70,0,0" Height="30" ContextMenu="{StaticResource ResourceKey=contextMenu}">Right Click Me</TextBox>
        <TextBox Width="100" Height="30" Text="TESTING BUG" Name="txtToSelect" />
    </Grid>
</Window>
And the code behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, ActiproSoftware.Windows.Controls.Ribbon.Controls.ExecuteRoutedEventArgs e)
        {
            txtToSelect.SelectAll();
            txtToSelect.Focus();
        }
    }
So if i open the contextMenu, and press the button, the txtToSelect suppose to get focused, but that's not working.

If you still need it in a zip file, let me know.
Thanks,
Miky.
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Miky,

We did some breakpoints and it seems something is setting focus back into the textbox with the ContextMenu after you code executes. The problem is that none of the call stack items are our code, they are all Microsoft's code. So it's difficult to say why that is happening compared to the native ContextMenu implementation (we even inherit the native ContextMenu).

I did find a workaround for you though... import the "System.Windows.Threading" namespace and add this instead:
this.Dispatcher.BeginInvoke(DispatcherPriority.Input, (DispatcherOperationCallback)delegate { 
    txtToSelect.SelectAll();
    txtToSelect.Focus();
    return null; 
}, null);


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.