![](/img/trans.png)
[英]WPF Control hosted Winforms using WPF ElementHost has Mouse Wheel Issue under Win 7 Only
[英]WPF control not displaying in ElementHost in WinForms app
我有一个WPF控件的问题,我试图在WinForms应用程序中的ElementHost中托管。 该控件是一个无形的自定义控件,我最初是在一个单独的测试项目中开发的,它是一个WPF应用程序。 在那里它显然工作正常,但在我的WinForms应用程序中,我得到的是一个空白的灰色框,其中显示ElementHost。
这是我用于创建,填充和添加ElementHost到父Control的C#代码:
// This is my WPF control
m_TabHostPanel = new TabHostPanel();
m_ElementHost = new ElementHost
{
Child = m_TabHostPanel,
Dock = DockStyle.Top,
Height = 34
};
this.Controls.Add( m_ElementHost );
父控件包含在运行时根据需要添加和删除的其他WinForms控件。 这些都是单独托管,其Dock设置为DockStyle.Fill。 因此,每次添加一个时,我都会将ElementHost发送到Z-order的后面,以确保它正确呈现:
m_ElementHost.SendToBack();
因此,我知道我没有遇到空域问题,或类似的问题。
我想知道的一件事是这样的:在原始项目中,我所有无外观控件的样式被合并到App.xaml中的应用程序的资源字典中,如下所示:
<Application x:Class="WpfTestApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Application/UserInterface/DataTemplates/TabModelDataTemplate.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/HoverablePressableButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/MiniControlButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabCloseButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabScrollLeftButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabScrollRightButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabListDropDownButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabHostComboBoxStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabButtonStyle.xaml"/>
<ResourceDictionary Source="Application/UserInterface/Styles/TabHostPanelStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
我已将App.xaml迁移到我的WinForms项目,但构建操作设置为Page。 如果我将它设置为ApplicationDefinition,我得到一个错误,说应用程序有多个入口点,这是有道理的,但我想知道是否正在拾取样式等。 如果没有这可能解释为什么我得到一个空白的灰色矩形,我的控制应该是因为没有这些,没有什么可以定义它的外观。 所以也许问题是,我如何将这些样式放入我的WinForms应用程序中,以便我的WPF控件可以看到它们?
我可能还应该提到这是在.NET Fx 3.5上运行的。
无论如何,现在我很困惑,所以任何帮助都会感激不尽。
非常感谢!
巴特
感谢您的回复,但我想您可能会误解我:我正在尝试使用自定义元素,其资源通常位于Application对象中,而不是将应用程序本身插入到ElementHost中。
幸运的是,我找到了答案:
http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/
精简版:
然后一切都很好:我的WPF控件显示应该如此。
现在,为什么我在发布到StackOverflow 之后才找到答案?
再次感谢,
巴特
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.