Version

Binding Data

Before You Begin

The Data property of the XamBarcode™ family of controls, is what you use to assign the value that you wish to be encoded. The Data property is a dependency property and you can use data binding to set its value.

What You Will Accomplish

You will bind data to the Data property of the XamCode128Barcode control.

Follow these Steps

  1. Add a XamCode128Barcode control to your page . For more information on this, refer to these topics:

  1. Create a class named BarcodeData that implements the INotifyPropertyChanged interface and add a Data property, which will provide notification if its value has changed:

In Visual Basic:

Imports System.ComponentModel

Public Class BarcodeData Implements INotifyPropertyChanged
    Public Sub New()
        Data = String.Empty
    End Sub
    Private barcodeData As String
    Property Data() As String
        Get
             Return barcodeData
        End Get
        Set(ByVal value As String)
             barcodeData = value
             RaisePropertyChanged()
        End Set
    End Property
    Public Event PropertyChanged(ByVal sender As Object, ByVal e
        As PropertyChangedEventArgs) Implements
              INotifyPropertyChanged.PropertyChanged
    Private Sub RaisePropertyChanged()
        RaiseEvent PropertyChanged(Me, New
                PropertyChangedEventArgs("Data"))
    End Sub
End Class

In C#:

using System.ComponentModel;

public class BarcodeData : INotifyPropertyChanged
{
    public BarcodeData()
    {
        Data = string.Empty;
    }
    private string barcodeData;
    public string Data
    {
        get
        {
            return barcodeData;
        }
        set
        {
            barcodeData = value;
            RaisePropertyChanged();
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged()
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
            new PropertyChangedEventArgs("Data"));
        }
    }
}
  1. Assign an instance of the BarcodeData class to the control’s DataContext property:

In Visual Basic:

Dim barcodeData As New BarcodeData
Barcode.DataContext = barcodeData

In C#:

var barcodeData = new BarcodeData();
Barcode.DataContext = barcodeData;
  1. Bind the control to the property:

In XAML:

<ig:XamCode128Barcode x:Name="Barcode" Data="{Binding Data}" />
  1. Assign a value to the BarcodeData instance’s Data property:

In Visual Basic:

barcodeData.Data = "324654"

In C#:

barcodeData.Data = "324654";
Note
Note

This example assigns a hard-coded value to the control. However, in a real-world application the value can be assigned from a database, some application logic, or from a barcode scanner. Regardless of where you get the value from, this example shows how the value is assigned to the control. Whenever the Data property of the BarcodeData class changes the control will show the change.

  1. Save and run your application.

xamBarcode DataBinding 01.png