【问题标题】:WPF Forms Integration - A Problem with GridWPF 表单集成 - 网格问题
【发布时间】:2021-08-05 23:00:40
【问题描述】:

我正在进行一项可行性研究,以了解我们是否以及如何将 Forms 应用程序集成到 WPF 项目中。

我从一个简单的例子开始,马上就遇到了问题(这里是代码):

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

在一个有 4 行的网格中,我放置了 4 个表单按钮。当我运行代码时,内部网格跨越边界并覆盖顶部和底部的标签(指示菜单和状态栏)并占据整个窗口的高度。

有谁知道这种行为以及如何解决它?

【问题讨论】:

    标签: wpf forms grid integration


    【解决方案1】:

    提示:尝试更大的高度值(您有 Height="450"),因为您的元素的高度总和刚好更高 (200 * 4),而且不要忘记标签。

    【讨论】:

    • 感谢您指出这一点。但问题不同:它应该滚动!滚动时,网格的边界被破坏,按钮移动到标签中。这只是针对大型项目的研究,增加窗户的高度是不够的。
    【解决方案2】:

    我在这里找到了解决方案:

    WindowsFormsHost ZOrder

    我在这里读到: 在 WPF 用户界面中,您可以更改元素的 z 顺序以控制重叠行为。托管的 Windows 窗体控件在单独的 HWND 中绘制,因此它始终绘制在 WPF 元素之上。

    但是这篇文章也包含一个带有解决方案的答案!

    你可以做一个小技巧。当您声明一个 WindowsFormsHost 时,它的父级是第一个 HWND 组件。通常它是根窗口。因此,控件的剪辑区域是整个窗口。 但是有一种方法可以创建“中间”HWND 项目以在 ScrollViewer 上剪辑 WinForms 区域。只需使用 ElementHost 放置另一个 WindowsFormsHost。

    感谢您的回答,ds1709。

    【讨论】:

      猜你喜欢
      • 2016-12-24
      • 1970-01-01
      • 2011-04-21
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多