Like any WinForms control, C1Barcode for WinForms can be used within a WPF project. C1Barcode supports over 10 linear and 2D encoding types (QRCode) and can add barcode displays to any of your WinForms or WPF applications. For using the control in WPF you have a couple different options available to you. The first and easiest option is to use a WindowsFormsHost element to host the Windows Forms control directly on a WPF page. The process could look something like this.
Now let's see how to do each of these steps. For #1 this is easy. Just browse to the install bin of Studio for WinForms 4.0 (Program Files/ComponentOne/Studio for WinForms/bin/v4) to add the reference. For #2 you can do this in code or in XAML. In code:
'Create the interop host control. Dim host As New System.Windows.Forms.Integration.WindowsFormsHost() 'Create the C1Barcode control. Dim barcode As New C1.Win.C1BarCode.C1BarCode barcode.Text = "C1Barcode" barcode.CodeType = C1.Win.C1BarCode.CodeTypeEnum.Code39 'Assign the C1Barcode control as the host control's child. host.Child = barcode ' Add the interop host control to the Grid control's collection of child controls. Me.grid1.Children.Add(host)
Or in XAML:
<WindowsFormsHost Name="_c1BarCodeHost" > <c1BarCode:C1BarCode xmlns:c1BarCode="clr-namespace:C1.Win.C1BarCode;assembly=C1.Win.C1BarCode.4" Text="Hello" CodeType="Code128" /> </WindowsFormsHost>
Step #3 is manually adding the licensing for C1Barcode. Normally the licenses.licx file is added by Visual Studio automatically when you drag a control onto the form. But in this scenario, the control is created without the toolbox, so Visual Studio doesn’t get a chance to do it for you. Adding the licenses.licx file is easy, it’s just a text file built as an embedded resource containing the names of the licensed controls and their assemblies, for example:
You can use the steps above to put ANY WinForms control in WPF. It's very straightforward and easy to understand. You can download some samples which demonstrate the above code in this forum post.
After I created those samples I did some more research and found an even better approach for using the WinForms C1Barcode control in WPF. This approach defines a wrapper element that derives from Image and encapsulates the WinForms control. The wrapper elements are true WPF elements, with support for binding, MVVM, layout, etc. In this regard it's a better approach. In this sample, the main page shows six barcode controls bound to three TextBox controls. There is no custom code-behind at all, and no WindowsFormsHost control. Download Sample