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

  • Question

  • 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:

    System.InvalidOperationException: Hosted HWND must be a child window of the specified parent. at System.Windows.Interop.HwndHost.BuildWindow(HandleRef hwndParent)
    at System.Windows.Interop.HwndHost.BuildOrReparentWindow() at System.Windows.Interop.HwndHost.OnSourceChanged(Object sender, SourceChangedEventArgs e) at System.Windows.SourceChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) at System.Windows.PresentationSource.UpdateSourceOfElement(DependencyObject doTarget, DependencyObject doAncestor, DependencyObject doOldParent)
    at System.Windows.PresentationSource.OnVisualAncestorChanged(DependencyObject uie, AncestorChangedEventArgs e) at System.Windows.UIElement.OnVisualAncestorChanged(Object sender, AncestorChangedEventArgs e) at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args) at System.Windows.Media.Visual.FireOnVisualParentChanged(DependencyObject oldParent) at System.Windows.Media.Visual.AddVisualChild(Visual child) at System.Windows.Media.VisualCollection.ConnectChild(Int32 index, Visual value) at System.Windows.Media.VisualCollection.Add(Visual visual) at System.Windows.Controls.UIElementCollection.AddInternal(UIElement element) at System.Windows.Controls.UIElementCollection.Add(UIElement element)

    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,

    HostPanelSystem.Windows.Controls.Grid

    ChildrenUIElementCollection Panel

    Also, 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 partial class:

    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.

    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 towards resolving the issue will be greatly appreciated.

    Regards,

    Pratik Prakash



    Monday, April 15, 2019 2:03 PM

All replies

  • 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.


    Monday, April 15, 2019 8:00 PM
  • Hi Pratik Prakash,

    According to the description, the issue is much more related to WPF. I will move the thread to the forum: Windows Presentation Foundation (WPF). There will be someone more professional to solve this problem for you.

    Regards,

    Kyle


    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.

    Tuesday, April 16, 2019 1:54 AM

  • 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:

    >>However, is there a way to make the above-mentioned code DPI aware and to get the form controls rendered without any issue?

    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 Lu

    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.

    Tuesday, April 16, 2019 6:36 AM
  • Hi Yong,

    It would have been great if you could have moved the thread to Outlook for Developers or Visual Studio Tools for Office (VSTO) instead of moving it to Off-topic posts.

    I couldn't do it as I believe it can be done only by moderators/admin.


    Thanks,

    Pratik Prakash

    Tuesday, April 23, 2019 1:30 PM
  • Hi  Pratik Prakash, 

    For some reasons, you need to start a new thread with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.

    Visual Studio Tools for Office (VSTO)

    Thank you for your understanding.

    Best Regards

    Yong Lu


    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 24, 2019 1:30 AM