Click here to Skip to main content
15,400,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
In MainWindow.xaml:
<avalonDock:LayoutDocument x:Name="docBlock1" ContentId="contentBlock1" Title="Block 1">
       <StackPanel x:Name="spBlock1" Loaded="spBlock1_Loaded" />

It is a 'Block1' tab window inside 'DocingManager' of MainWindow. I have implemented AvalonDock inside MainWindow for docking purposes.

In MainWindow.xaml.cs

private void spBlock1_Loaded(object sender, RoutedEventArgs e)
                    WindowInteropHelper windowHwnd = new WindowInteropHelper(this);
             W5.EditLD _m_Ctrl = new W5.EditLD(); //this is third party control.
             IntPtr handleD = _m_Ctrl.GetHandle();
             IntPtr hWnd = windowHwnd.Handle;
             //_m_Ctrl.CreateWnd() creates control above the Mainform. 
             bCreate = _m_Ctrl.CreateWnd(WinAPI.CWnd.WS_VISIBLE | WinAPI.CWnd.WS_BORDER | WinAPI.CWnd.WS_CHILD, 0, 0, 100, 100, hWnd, IDC_MAIN); //It's Definition: public bool CreateWnd(UInt32 dwStyle, int x, int y, int width, int height, IntPtr hWndParent, UInt32 iID)            

private void spBlock1_SizeChanged(object sender, SizeChangedEventArgs e)
 //_m_Ctrl.MoveWindow() will move this control as per MainWindow resizes.
             _m_Ctrl.MoveWindow(279, 245, 1300, 740); //Here, I have passed Window's x, y, width & height parameter manually. It's Definition: public void MoveWindow(int iX, int iY, int iWidth, int iHeight)

We have DLL of this control. We don't have any source code for this control. My application is in WPF C#. I am calling WindowInteropHelper() inside my WPF project's event. The purpose of this method is to add one user control over the the form.
But this WindowInteropHelper() takes only one parameter and it must be 'Window' parameter. Please, see below-attached image.
Is it possible to add dynamically x, y, width & height parameter ( from screen (0,0) point ) of 'spBlock1' or 'docBlock1' to change this control according to resizing of the 'spBlock1' or 'docBlock1' ?
Please, see below-attached image.

See Method Signature

What I have tried:

But I want to use the same control over my 'Block 1' tab. But in this WindowInteropHelper() method call, I have passed 'this' keyword. 'this' is pointing to MainWindow. Hence control will see outside of the 'Block 1' tab. Is it possible to show this control inside the 'Block 1' tab? How can I achieve it? Is it possible to add another 'Window' form inside that 'Block 1'? Then I will pass this added new form in place of 'this' keyword to achieve my task.
Thanks in advance.
Updated 14-Jun-21 9:07am

1 solution

Realistically, no WPF doesn't really support embedding windows inside other windows. I believe Microsoft decided that having this sort of hierarchy (MDI) was bad practise, so generally the idea is that you create user controls for the content.

For what purpose do you need the WindowInteropHelper[^]? If it's to retrieve the handle of the window once embedded in your parent window, then that's likely not going to be possible so you probably don't need it.

If you're after resolving handles for elements in a window, you need to know that WPF doesn't work like a standard WinForms application. All elements in a WPF window are part of a visual hierarchy, so identifying the handle for a user control or similar will simply return the handle of the parent window.

As far as your AvalonDock solution is concerned, I believe they have their own mechanism to produce new windows if a floating window is required. In the event that the control does this, you could probably then use the WindowInteropHelper to target the new floating window.
Dinesh Kudale 14-Jun-21 15:00pm
@Chris Copeland, Thanks for your reply.

From your above discussion, I have understood below points:
1) Window inside another window- Not possible. It is bad practice. Hence it is not possible.
2) Solution: Create User control for the content. But the user control handler points to the Parent window. Hence, it is useless for my issue.
3) Avalondock has its own mechanism to produce new windows. Due to this, probably you can use WindowInteropHelper() to target this window.

For 1st and 2nd points, You are saying right.
For the 3rd point, I have also tried this option but when I will try to pass the AvalonDock window as a parameter to WindowInteropHelper() method, then it will arise below error:
Error: "Window must be the root of the tree. Cannot add Window as a child of Visual."
Till now searching for this solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900