我正在尝试在WPF应用中使用WindowsFormsHost,因此我可以使用一些Windows窗体组件。

<Grid>
    <WindowsFormsHost>

    </WindowsFormsHost>
</Grid>

这一切都正常,没有错误,但是当我从VS2008中的工具箱中拖动Windows窗体组件时,它们都变灰了。在某种程度上,这是有道理的,因为只有WPF组件可用。但是,如果Windows窗体组件全部变灰并且不可用,那么如何将它们放到WindowsFormsHost上呢?

有帮助吗?

解决方案

我认为WPF设计器不支持Windows Forms组件。您需要在XAML或代码中指定这些。一个简单的示例,说明如何将WinForms按钮添加到WPF应用程序中。请注意,该按钮在设计图面上不可见。

<Window x:Class="DataTemplateBind.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    Title="Window1" Height="300" Width="300">

    <WindowsFormsHost Height="200" Name="windowsFormsHost1" Width="258" >
        <forms:Button Text="Waaa :("></forms:Button>
    </WindowsFormsHost>
</Window>

请注意添加的 xmlns:forms 命名空间,默认情况下不存在。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top