TheSwamp

Code Red => .NET => Topic started by: Jeff_M on February 21, 2023, 04:55:02 PM

Title: WPF Window which opens another Window
Post by: Jeff_M on February 21, 2023, 04:55:02 PM
I hope I can explain this so someone might be able to help. I have a WPF Window which I open in AutoCAD, in that window is a DataGrid, when a cell is dbl. clicked it opens another Window in which the user may type or paste text. If the user browses away from AutoCAD to get the text to copy, upon returning the second window is hidden and does not appear on the taskbar (the first window does show). This makes it appear that the first window is frozen and the user is unable to do anything. However, using ALT-TAB the second window is available to select there, selecting it allows everything to continue on.

What I have been unable to find is a way for that second window to have focus when the user switches back to the AutoCAD session. Any and all suggestions welcomed.
Title: Re: WPF Window which opens another Window
Post by: Jeff_M on February 21, 2023, 06:09:54 PM
Gah! Of course when I create a small test project to demonstrate the issue, it works as expected. I will now try to see what I did differently between them.
Title: Re: WPF Window which opens another Window
Post by: kdub_nz on February 21, 2023, 07:05:27 PM
That situation is related to Murphy's Law.  ;-)

The test case I mean :)
Title: Re: WPF Window which opens another Window
Post by: Atook on February 21, 2023, 08:33:44 PM
Maybe the second form should be modal?
Title: Re: WPF Window which opens another Window
Post by: Jeff_M on February 21, 2023, 10:20:41 PM
Yes, Kdub, it certainly is.

Atook, thanks for the suggestion, both Windows are already modal. I did try making the second one modeless and it disappears as soon as it comes up.
Title: Re: WPF Window which opens another Window
Post by: n.yuan on February 23, 2023, 09:26:41 AM
Jeff, I do have same or similar experiences occasionally with WPF windows (while being shown as modal dialog), it could even happen with just one modal WFP dialog (not as your description of second dialog window opened from an modal dialog.

I assume you open both dialog windows with Application.ShowModalWindow(). I think the issue might be because of how ShowModalWindow() is implemented. In stand-alone WFP app, the main window can be set as the owner of the second window (modeless or modal). I suppose in Application.ShowModalWindow() set AutoCAD's main window as the dialog winodw's owner, if the first argument is not supplied.

So, I think you could try these 2 things to see if they help:
1. When showing second level dialog, supply the first dialog window as owner argument in the ShowModalWindow() method. Well, this may force you to have a window's reference in your first window's ViewModel, if you use MVVM pattern, which usually makes "pure MVVM embracers" feel annoyed.
2. You can try to simply show the second level dialog with Window.ShowDialog() (with or without owner argument supplied), instead of Application.ShowModalWindow(). That is, only do this with the dialog window opened on top of a dialog window. I have been using this approach for the rare ocassion when a second level of dialog is needed.
Title: Re: WPF Window which opens another Window
Post by: Jeff_M on February 23, 2023, 12:48:38 PM
Norman, thank you!

Seeing as this is my first real MVVM/WPF implementation I am not aware of all the ways to do things. I'll address your second suggestion first: I had already tried that with identical results.
You first suggestion opened my eyes to the fact the ShowModalWindow has multiple overloads. With the minor addition of "this, " the code now works MOSTLY like it should. I say mostly because the second window still is not shown on the taskbar, but it at least is now on top of the first form when returning to AutoCAD so it can be easily selected to paste the copied text. This is within the Double_Click event of the Datagrid
Code - C#: [Select]
  1.             var editWindow = new EditWindow(text);
  2.             var windowResult = AcAppSvc.Application.ShowModalWindow(this, editWindow);
  3.  

Thanks again!
Title: Re: WPF Window which opens another Window
Post by: 57gmc on February 23, 2023, 03:36:53 PM
I say mostly because the second window still is not shown on the taskbar,
Dumb question...does the second form have an icon assigned to it? In the Common section of the Properties palette, you can assign an icon to each form.
Title: Re: WPF Window which opens another Window
Post by: Jeff_M on February 23, 2023, 04:47:50 PM
No it doesn't. But neither does the one in my test project which does show in taskbar.
Title: Re: WPF Window which opens another Window
Post by: It's Alive! on February 23, 2023, 11:29:18 PM
Seems DataGrid is always causing problems, I think it likes to hog events  :crazy2:
maybe you can make your own message hook and listen for something like  WM_ACTIVATE & WA_CLICKACTIVE
send some sort of set focus when autocad is activated