Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

.net - WPF control not displaying in ElementHost in WinForms app

I have a problem with a WPF control that I'm trying to host in an ElementHost in a WinForms app. The control is a lookless custom control that I originally developed in a separate test project, which was a WPF app. In there it obviously works fine, but in my WinForms app all I get is a blank grey box where the ElementHost is displayed.

Here's my C# code for creating, populating, and adding the ElementHost to the parent Control:

// 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 );

The parent control contains other WinForms controls that are added and removed at runtime, as needed. These are all hosted singly with their Dock set to DockStyle.Fill. Thus, every time I add one I send the ElementHost to the back of the Z-order to make sure it renders correctly:

m_ElementHost.SendToBack();

Thus, I know I'm not running into an airspace problem, or anything like that.

The one thing I did wonder about is this: in the original project the styles for all my lookless controls were merged into the resource dictionary for the application in App.xaml, like this:

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

I've migrated App.xaml to my WinForms project, but the build action is set to Page. If I set it to ApplicationDefinition I get an error saying that the application has multiple entry points, which makes sense, but I'm wondering then if the styles, etc., are being picked up. If not this might explain why I'm getting a blank grey rectangle where my control should be because, without these, there's nothing to define its look. So maybe the question is, how do I get these styles into my WinForms application so that my WPF controls can see them?

I should probably also mention that this is running on .NET Fx 3.5.

Anyway, for now I'm perplexed, so any help would be gratefully received.

Many thanks!

Bart

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks for replying, but I think you may misunderstand me: I'm trying to use a custom element, whose resources are normally in the Application object, not insert the application itself into the ElementHost.

Fortunately, I've found an answer:

http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/

Short version:

  • Set build action for App.xaml to Page
  • In the code behind for App.xaml create a default constructor that just calls InitializeComponent()
  • When the WinForms app starts up, just create an instance of the App class.

And then it's all good: my WPF control appears as it should.

Now, why is it I only find the answer after I've posted to StackOverflow?

Thanks again,

Bart


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...