How to add a WinForm control to WPF so that I can refer to it in the xaml.cs file

I am moving part of the WinForms project to WPF.

I want to add an existing WinForms user control to a WPF form. The WinForm user control is called "TicketPrinter" and lives in the same project as the WPF form.

In my xaml, I have this line:

xmlns:Printers="clr-namespace:Project.UserControls.Printers"

And then I use it in my haml here:

        <WindowsFormsHost Height="430" HorizontalAlignment="Left" Margin="468,12,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="324">
            <Printers:TicketPrinter Printers:Name="ZapTicketPrinter">
            </Printers:TicketPrinter>
        </WindowsFormsHost> 
    </Grid>
</Window>

When I run the project, the user control appears on the form as expected.

But when I go to the code behind the xaml.cs file and try to access "ZapTicketPrinter", it is not available as a link.

i.e.

I am trying to use ZapTicketPrinter and it is not recognized.

I also tried the following:

TicketPrinter ticketPrinter = this.FindName("ZapTicketPrinter") as TicketPrinter;

but we get zero

What am I missing? How do I specify a name in my code?

+5
2

x: :

<WindowsFormsHost>
    <Printers:TicketPrinter x:Name="ZapTicketPrinter"/>
</WindowsFormsHost>

MSDN

http://msdn.microsoft.com/en-us/library/ms751761.aspx
: Windows Forms WPF

xaml
http://msdn.microsoft.com/en-us/library/ms742875.aspx
: Windows Forms WPF XAML

+7

:

private void LoadWFUserControl() 
{
    // Initialize a Host Control which allows hosting a windows form control on WPF. Ensure that the WindowsFormIntegration Reference is present.
    System.Windows.Forms.Integration.WindowsFormsHost host =
        new System.Windows.Forms.Integration.WindowsFormsHost();

    // Create an object of your User control.
    MyWebcam uc_webcam = new MyWebcam();

    // Assign MyWebcam control as the host control child.
    host.Child = uc_webcam;

    // Add the interop host control to the Grid control collection of child controls. Make sure to rename grid1 to appr
    this.grid1.Children.Add(host);
}
+3