0

I am doing a feasibility study to find out whether and how we can integrate a Forms application into a WPF project.

I started with a simple example and immediately encountered a problem (here is the code):

<Window x:Class="TestFormsIntegration.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"  
        xmlns:local="clr-namespace:TestFormsIntegration"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="40"/>
        </Grid.RowDefinitions>
        <Label Grid.Row="0" Content="Menue Bar" HorizontalContentAlignment="Center" Background="BlanchedAlmond" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        <ScrollViewer x:Name="scrollViewer" Grid.Row="1" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
            <Grid >
                <Grid.RowDefinitions>
                    <RowDefinition Height="200" />
                    <RowDefinition Height="200" />
                    <RowDefinition Height="200" />
                    <RowDefinition Height="200" />
                </Grid.RowDefinitions>
                <WindowsFormsHost Grid.Row="0" HorizontalAlignment="Center" Height="190" VerticalAlignment="Center" Width="250">
                    <wf:Button Text="Button 0" Height="180" Width="200" BackColor="Gray" />
                </WindowsFormsHost>
                <WindowsFormsHost Grid.Row="1" HorizontalAlignment="Center" Height="190" VerticalAlignment="Center" Width="250">
                    <wf:Button Text="Button 1" Height="180" Width="200" BackColor="Gray"/>
                </WindowsFormsHost>
                <WindowsFormsHost Grid.Row="2" HorizontalAlignment="Center" Height="190" VerticalAlignment="Center" Width="250">
                    <wf:Button Text="Button 2" Height="180" Width="200" BackColor="Gray" />
                </WindowsFormsHost>
                <WindowsFormsHost Grid.Row="3" HorizontalAlignment="Center" Height="190" VerticalAlignment="Center" Width="250">
                    <wf:Button Text="Button 3" Height="180" Width="200" BackColor="Gray" />
                </WindowsFormsHost>

            </Grid>
        </ScrollViewer>
        <Label Grid.Row="2" Content="Status Bar" HorizontalContentAlignment="Center" Background="Wheat" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </Grid>
</Window>

In a grid with 4 rows I place 4 forms buttons. When I run the code, the inner grid crosses the boundaries and overwrites the labels at the top and bottom (indicating Menu and Status Bar) and takes up the height of the entire window.

Does anyone know this behaviour and how to fix it?

2 Answers2

0

Hint: Try a larger value for height (you have Height="450"), because the sum of the height of your elements is just higher (200 * 4) and don't forget the labels.

makO
  • 46
  • 1
  • 4
  • 1
    Thank you for pointing this out. But the problem is different: it should be scrolled! And when scrolling, the boundaries of the grid are violated and the buttons move into the labels. This is only a study for a larger project, where it is not enough to increase the height of the window. – Lucillo May 17 '21 at 08:52
0

I have found the solution here:

WindowsFormsHost ZOrder

Here I read: In a WPF user interface, you can change the z-order of elements to control overlapping behavior. A hosted Windows Forms control is drawn in a separate HWND, so it is always drawn on top of WPF elements.

But this article also contains an answer with a solution!

You can do a little trick. When you declare an WindowsFormsHost, it's parent is first HWND component. Usually it's root window. So, clip area for controls is whole window. But there's a way to create "intermediate" HWND item to clip WinForms area over ScrollViewer. Just place another WindowsFormsHost with ElementHost.

Thank you for your answer, ds1709.