与ElementHost对接

发布于 2024-07-23 06:34:00 字数 1167 浏览 8 评论 0原文

我正在尝试将 WPF 用户控件添加到现有的 WinForms 项目中,并使 WPF 用户控件停靠并填充整个空间。

当前有一个框架可以将 WinForms UserControls 加载到父窗体(面板)中以响应按钮单击。 这就是我试图挂钩的地方 - 当前加载的 WinForms UserControl 将具有 ElementHost。

层次结构:

  • Form1.cs - 包含一个获取 WinForms UserControls 动态加载的面板
    • WinForms UserControl - 包含 ElementHost
      • WPF UserControl

ElementHost 将 Dock 设置为 Fill,将其 Child 属性设置为 ucReport,这是一个 WPF UserControl,具有以下标记(仅包括顶层设计):

<UserControl x:Class="MyClassName"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TabControl HorizontalAlignment="Left" Name="tabControl1">
        <TabItem Header="Header1">
           ...The interesting stuff goes here
        </TabItem>
    </TabControl>
</UserControl>

当我调整表单大小时,UserControl 的内容确实会垂直扩展,但在水平方向上,内容只会扩展得足以容纳其内容。

当我在设计器中查看 WinForms UserControl(具有 ElementHost 的那个)时,问题就很明显了。 指定的 WPF 内容正在呈现,并且从上到下填充,但不是从左到右填充。

我认为必须在 XAML 中设置某些内容(可能在 UserControl 声明上?)才能让它填充其父容器,即 ElementHost - 我只是找不到该属性。

有人能启发我吗?

I am attempting to add a WPF usercontrol to an existing WinForms project and get the WPF UserControl to dock and fill the entire space.

There's a current framework that loads WinForms UserControls into a parent form (into a panel) in response to button clicks. This is where I'm trying to hook in - The WinForms UserControl that's currently getting loaded will have the ElementHost.

Hierarchy:

  • Form1.cs - contains a panel that gets WinForms UserControls dynamically loaded
    • WinForms UserControl - contains the ElementHost
      • WPF UserControl

The ElementHost has Dock set to Fill and its Child property set to ucReport, which is a WPF UserControl, which has the following markup (only top level design included):

<UserControl x:Class="MyClassName"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <TabControl HorizontalAlignment="Left" Name="tabControl1">
        <TabItem Header="Header1">
           ...The interesting stuff goes here
        </TabItem>
    </TabControl>
</UserControl>

The content of the UserControl does expand vertically when I resize the form, but horizontally, the content only expands large enough to accomodate its content.

When I view the WinForms UserControl (the one that has the ElementHost) in the designer, the problem is apparent. The WPF content that's specified is getting rendered and it's filled top to bottom, but not left to right.

I'm of the mind that it's something in the XAML that has to be set (perhaps on the UserControl declaration?) to get it to fill it's parent container, which is the ElementHost - I just can't find the property.

Would someone enlighten me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2024-07-30 06:34:00

将 Horizo​​ntalAlignment 更改为 Stretch 或完全删除它。

Change HorizontalAlignment to Stretch or get rid of it entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文