I am migrating part of a WinForms project into WPF.
I want to add an existing WinForms User Control into 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 xaml 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 into the code behind xaml.cs file and try to access "ZapTicketPrinter" it is not available as a reference.
i.e.
I try using ZapTicketPrinter and it's not recognised.
I've also tried the following:
TicketPrinter ticketPrinter = this.FindName("ZapTicketPrinter") as TicketPrinter;
but get a null
What am I missing? How do I reference the name in my code?
Yes you can, both Windows Forms within a WPF application, and WPF controls within Windows Forms.
provide x:Name instead of printer:name
<WindowsFormsHost>
<Printers:TicketPrinter x:Name="ZapTicketPrinter"/>
</WindowsFormsHost>
MSDN Sample
Using code behindhttp://msdn.microsoft.com/en-us/library/ms751761.aspx
Walkthrough: Hosting a Windows Forms Control in WPF
Using xamlhttp://msdn.microsoft.com/en-us/library/ms742875.aspx
Walkthrough: Hosting a Windows Forms Control in WPF by Using XAML
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With