Asked by:
Adding a WPF UI Element to the Winform Grid Control Panel throw InvalidOperationException: Hosted HWND must be a child window of the specified parent.

Question
-
Hi,
We have a CRM VSTO Add-in for Microsoft Outlook, which displays a custom task pane which has a WinForm control inside a WPF form/control as one of its functionalities. But, since after Version 1812 and the latest update to Office 365 pro plus installations, in some of the client machines we have been facing the following error/exceptions and form doesn't show or load.
Basically, the code fails while embedding and displaying the WPF form. The exact code snippet causing the issue is as follows:
public void DisplayForm(System.Windows.Forms.Control form) { // Adapt WinForm Form Control to WPF System.Windows.Forms.Integration.WindowsFormsHost wfh = new System.Windows.Forms.Integration.WindowsFormsHost(); wfh.Child = form; wfh.Margin = new Thickness(0); // Display WPF Form DisplayForm(wfh); } // Embed a WPF Control public void DisplayForm(UIElement form) { HostPanel.Children.Clear(); //***Below line throws an exception*** HostPanel.Children.Add(form); }
In the above code,
HostPanel: System.Windows.Controls.Grid
Children: UIElementCollection Panel
It is really difficult to narrow down to the exact root cause of the issue when everything was working as expected and just after pushing the updates to the Microsoft Office, the errors/exceptions are being seen.
Any help or pointers for the investigation or on resolving the issue will be greatly appreciated.
Thanks in advance !!
Regards,
Pratik Prakash
- Edited by Pratik Prakash Monday, April 15, 2019 8:01 PM
- Moved by Yong LuMicrosoft contingent staff Tuesday, April 23, 2019 8:49 AM
Tuesday, April 9, 2019 8:13 PM
All replies
-
Hi Pratik,
According to your description, I guess that you want to use Winform control in wpf window, am I right? If yes, I think you can host Winform control in wpf by xaml, there are some article that you can take a look:
https://www.c-sharpcorner.com/uploadfile/mahesh/using-windows-forms-controls-in-wpf/
Best Regards,
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, April 10, 2019 2:54 AM -
Hi Cherry,
Thank you for your reply!
In the existing design, the HostPanel or the grid has been used/placed in the XAML like below :
<Grid> <!-- Form Panel --> <Grid Name="HostPanel" Margin="0,50,0,0"> </Grid>
And, in the XAML code part:
1. We are assigning the Winform control as a child of the WPF.
2. Then, we are trying to add the WPF form/UIElement in the UIElementCollection Panel of the grid control.
- Edited by Pratik Prakash Wednesday, April 10, 2019 10:51 AM
Wednesday, April 10, 2019 6:31 AM -
This issue seems to be due to Microsoft introduced Office support for high definition displays
And the issue could be fixed inside Outlook by selecting "Optimize for compatibility" in Display Settings on the bottom status bar or in User Interface options under General Outlook options:
However, is there a way to make the above-mentioned code DPI aware and to get the form controls rendered without any issue?
Also, from this MSDN article, I learn that it might not be possible as child parented to Office window cannot respond to DPI scaling. But, any further inputs on the same would be highly appreciated.
Thanks.
- Edited by Pratik Prakash Monday, April 15, 2019 7:50 PM
Monday, April 15, 2019 7:43 PM -
Hi Pratik Prakash,
>>We have a CRM VSTO Add-in for Microsoft Outlook, which displays a custom task pane which has a WinForm control inside a WPF form/control as one of its functionalities. But, since after Version 1812 and the latest update to Office 365 pro plus installations, in some of the client machines we have been facing the following error/exceptions and form doesn't show or load.
From your description, I suggest you can to go to the Outlook for Developers or Visual Studio Tools for Office (VSTO) for getting suitable help.
Thank you for your understanding.
Best Regards
Yong LuMSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, April 17, 2019 9:29 AM