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 XamQRCodeBarcode control.

Follow these Steps

  1. Add a XamQRCodeBarcode control to your application . 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 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 BindingContext property:

In C#:

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

In XAML:

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

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.