WPF focus after TaskDialog.Show

WPF focus after TaskDialog.Show

colin_magner
Advocate Advocate
2,562 Views
4 Replies
Message 1 of 5

WPF focus after TaskDialog.Show

colin_magner
Advocate
Advocate

I've got a WPF addin, which uses TaskDialog.Show to inform the user of specific circumstances.

 

After using TaskDialog.Show the WPF Window loses focus, and you cannot set focus by clicking on it, meaning you cannot move or resize it.  While it is in this state the controls on the window are still active and highlight on a mouseover as if the window still has focus, and you can click on them and they activate.  After using any of the controls the window focus behavior returns to normal.  I can also return normal behavior by focussing on something other than Revit (e.g. Chrome, VS, IE, Explorer, etc. etc).

 

Any why this is happening, or how I can set focus back to normal?  I've tried this.Activate() and this.UpdateLayout() but neither worked.

 

Thanks.

0 Likes
2,563 Views
4 Replies
Replies (4)
Message 2 of 5

augusto.goncalves
Alumni
Alumni
I believe this can be a reincarnation of Windows focus problems on WPF.

In most cases is related to loosing track of ownership between dialogs. If I'm understanding you right, there is the main Revit window, then your WPF form, then the TaskDialog.

When it loses focus, is it going somewhere? Can you type? Maybe the control is stealing focus and you can set Focusable=False. Additionally you can quickly try ensure your WPF dialog has the Owner property?
Regards,



Augusto Goncalves
Twitter @augustomaia
Autodesk Developer Network
0 Likes
Message 3 of 5

Anonymous
Not applicable

You need to like the wpf owner to Revit Main Window. You can try this:

 

MyWPF ui = new MyWPF();

 

Process revitProcess = Process.GetCurrentProcess();
//Get the UI corresponding to that process
IntPtr revitMainWindowPointer = revitProcess.MainWindowHandle;

//Convert the IntPtr to a class implementing IWin32Window 
WindowHandleHelper revit_window = new WindowHandleHelper(revitMainWindowPointer);
//Create a windowinterophelper from the view
WindowInteropHelper helper = new WindowInteropHelper(ui);
//Set the view owner as the Revit UI convert as IWin32Windows
helper.Owner = revit_window.Handle;


ui.ShowDialog();

 

 

 

With 

 

public class WindowHandleHelper : IWin32Window
{
IntPtr _hwnd;

public WindowHandleHelper(IntPtr h)
{
_hwnd = h;
}

public IntPtr Handle
{
get
{
return _hwnd;
}
}
}

 

Message 4 of 5

Troy_Gates
Advocate
Advocate

I add the following to all my WPF forms to make them a child window of Revit which will include dialogs. Place it after InitializeComponent(); 

 

 

// makes the WPF form a child of the Revit application
WindowInteropHelper helper = new WindowInteropHelper(this);
helper.Owner = Autodesk.Windows.ComponentManager.ApplicationWindow;

 

 

Message 5 of 5

arif.hanif
Contributor
Contributor

worked like a charm, thanks so much troy

0 Likes