Can't move focus away from ToolWindow in mouse-click event

Docking/MDI for WPF Forum

Posted 14 years ago by Adam Myers
Version: 10.1.0522
Avatar
Hello,

I have a window that is used to edit & run scripts in a locally-created language. I am using a combination of DocumentWindows (each containing an Actipro SyntaxEditor), and ToolWindows to provide support features, e.g. listing all declared functions. All of the ToolWindows that I am currently using include a Themed DataGrid.

I'm trying to get my "functions list" ToolWindow to show the function body in the appropriate code editor (DocumentWindow) when the user double-clicks on a corresponding row in the datagrid. Everything is working fine, except that the code editor does not have focus after the double-click is handled.

I have tried everything (this.Activate(true), docksite.ActivatePrimaryWindow(), etc.) to get focus to the editor (DocumentWindow), with no success. By watching System.Windows.Input.Keyboard.FocusedElement and stepping through my event handler, I finally discovered that the editor IS getting focus, but as soon as execution leaves the double-click event handler, Keyboard.FocusedElement is again set back to my ToolWindow!

Here is some sample code to illustrate the issue:

//
// Mouse Event handler in FunctionsView : ToolWindow
//
private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
   ObservableFunctionRow item = datagrid.SelectedItem as ObservableFunctionRow;

   // If item is not an ObservableFunctionRow, do nothing
   if (item == null) return;

   // Raise the function selected event, if there are any listeners
   if (FunctionSelected != null) FunctionSelected(this, new FunctionSelectedEventArgs(item.Function));
}

//
// Custom Event handler in ScriptWindow : Window
//
private void FunctionsView_FunctionSelected(object sender, FunctionSelectedEventArgs e)
{
   // A function was selected: jump to the location at which it is declared
   // NOTE: this opens a new editor DocumentWindow if necessary, and always
   // calls Activate() on it
   ShowCodeFile(e.SelectedFunction.Location);
}
I'm not sure if the problem is that I'm trying to change focus from within an event handler, or there's something I need to do to allow the ToolWindow to release focus, or something else I haven't considered. Any help would be appreciated.

Comments (1)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Adam,

I believe you need to set e.Handled to true, otherwise the mouse events will bubble up. One or more controls above the DataGrid maybe set the focus when clicked, like a TextBox. You should be able to add a handler for the ToolWindow.GotFocus or IsKeyboardFocusWithin event, and check the stack trace there. You may need to set the Hit Count on the break point, to prevent Visual Studio from breaking the first time you click though.


Actipro Software Support

The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.