¿La forma correcta de establecer la propiedad Parent de un control WinForms alojado en un proyecto C# WPF?

I have a C# WPF app that has a WinForms control hosted in a WindowsFormsHost container. When I run my app the WinForms control does nothing in its OnPaint() event handler because the ParentForm property is NULL. In a pure WinForms project, this property is not NULL and is set to the Form the control was drawn on.

In my WPF project, since I can't figure out how to get the WinForms control to appear in the Designer, I added the control via the project XAML file. The control appears when the project is run but as I said, it never repaints because its ParentForm property is NULL.

In the main form's Window_Loaded() event I tried setting the WinForms control Parent property to the top level Window in the XAML file. I did not use the ParentForm property since it is read-only. However the compiler gives me this error:

Cannot implicitly convert type 'RawDataTestApp.Window1' to 'System.Windows.Forms.Control'

I assume I'm getting this error because Window1 is not a control? In any case, I don't know how to properly attach the WinForms control to the main form in my WPF project. How should I be doing that? I've included the pertinent parts of the project's XAML file below. It's from an open source C# project. I added a WinForms graph control to the project hosted by a WindowsFormsHost container:

// Here's the source line that gets the error.  pdeAffectiv is the WinForms graphcontrol that is hosted in a WindowsFormsHost container:
    pdeAffectiv.Parent = MainForm;

// -------------------------- WPF Project XAML file ----------------------------------
<!-- Copyright © 2010 James Galasyn -->
<Window x:Name="MainForm" x:Class="RawDataTestApp.Window1"
    Title="EmotivEngine Realtime Client" Height="454" Width="1042" Background="#FF000033" Foreground="#FF33FFFF" xmlns:emoclient="clr-namespace:EmoEngineClientLibrary;assembly=EmoEngineClientLibrary"
    xmlns:local="clr-namespace:RawDataTestApp" Loaded="Window_Loaded" Closing="Window_Closing"

        <WindowsFormsHost x:Name="wfhAffectiv" HorizontalAlignment="Left" Height="148" Margin="530,75.493,0,0" VerticalAlignment="Top" Width="471" >
            <graphlib:PlotterDisplayEx x:Name="pdeAffectiv" >

preguntado el 23 de diciembre de 12 a las 15:12

It is very unclear why this control needs the ParentForm set, it is a property of a ContainerControl class. Containers are not in the business of painting anything elaborate and if there's anything it should do to match its parent's appearance then it should have used Parent instead. You can't magically up a Form in this context, if you can't fix the control then you are probably screwed. -

@HansPassant - Fortunately it's an open source control so I modified it to paint the control even if the ParentForm property is NULL if a WinFormsAdapter object is in the graph control's ancestor chain, indicating it may be in a WPF project. Feels like a kludge so hopefully there's a way to get the ParentForm property to a useful value. Thanks for your suggestion. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.