Skip to main content Skip to footer

Using Our Barcode Controls in WPF Apps

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.

  1. Add a reference to the C1.Win.C1Barcode.4 assembly in your WPF project.
  2. Set the Child property on a WindowsFormsHost element to an instance of a C1Barcode.
  3. Add the license for C1Barcode

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:

C1.Win.C1BarCode.C1BarCode, C1.Win.C1BarCode.4

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.

A Better WPF Approach

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

MESCIUS inc.

comments powered by Disqus