BarCode for WPF | ComponentOne
In This Topic
    BarCodeViewModel Class
    In This Topic

    This topic covers the DataSource used for the QuickStart topic, that is, the BarCodeViewModel class.

    C#
    Copy Code
    public class BarCodeViewModel:ViewModelBase
        {
            public int counter = 0;
            //List of Barcodetypes which are visible in combobox
            private List<CodeType> _barcodes;
            public List<CodeType> Barcodes
            {
                get { return _barcodes; }
                set
                {
                    _barcodes = value;
                    OnPropertChanged("Barcodes");
                }
            }
            //List of Products which are visible in combobox
          
            //List of Categories which are visible in combobox
            private List<string> _category;
            public List<string> Categories
            {
                get { return _category; }
                set { _category = value;OnPropertChanged("Categories"); }
            }
            #region Properties
            private string _barcodesChanged;
            public string BarcodesChanged
            {
                get { return _barcodesChanged; }
                set { _barcodesChanged = value;OnPropertChanged("BarcodeChanged");CreateBarCode(); }
            }
            private string _categoriesChanged;
            public string CategoriesChanged
            {
                get { return _categoriesChanged; }
                set { _categoriesChanged = value;
                    if(_categoriesChanged != null)
                    {
                        IsBarCodeTypeEnabled = true;
                        if (_categoriesChanged=="Email")
                        {
                            Barcodes = new List<CodeType>() { CodeType.QRCode, CodeType.DataMatrix, CodeType.Pdf417, CodeType.Code39x };
                            PageToShow = new Email();
                        }
                        else if(_categoriesChanged=="Text")
                        {
                            Barcodes = new List<CodeType>() { CodeType.Bc412, CodeType.Code11, CodeType.HIBCCode128, CodeType.HIBCCode39, CodeType.Iata25, CodeType.IntelligentMailPackage, CodeType.ISBN, CodeType.ISMN, CodeType.ISSN, CodeType.ITF14, CodeType.MicroQRCode, CodeType.Pharmacode, CodeType.Plessey, CodeType.PZN, CodeType.SSCC18, CodeType.Telepen, CodeType.QRCode, CodeType.DataMatrix, CodeType.Pdf417, CodeType.Code39x };
                            PageToShow = new Text();
                        }
                        else if(_categoriesChanged=="Url")
                        {
                            Barcodes = new List<CodeType>() { CodeType.QRCode, CodeType.DataMatrix, CodeType.Pdf417, CodeType.Code39x };
                            PageToShow = new Url();
                        }
                        else
                        {
                            Barcodes = new List<CodeType>() { CodeType.QRCode, CodeType.DataMatrix, CodeType.Pdf417, CodeType.Code39x };
                            PageToShow = new VCard();
                        }
                      
                    }
                    CreateBarCode();
                }
            }
        
            private object _pageToshow;
            public object PageToShow
            {
                get { return _pageToshow; }
                set { _pageToshow = value;OnPropertChanged("PageToShow"); }
            }
            private string _url;
            public string Url
            {
                get { return _url; }
                set { _url = value;OnPropertChanged("Url"); }
            }
            private string _text;
            public string Text
            {
                get { return _text; }
                set { _text = value;OnPropertChanged("Text"); }
            }
            private string _email;
            public string Email
            {
                get { return _email; }
                set { _email = value; OnPropertChanged("Email"); }
            }
            private CodeType _barCodeType;
            public CodeType BarCodeType
            {
                get { return _barCodeType; }
                set { _barCodeType = value;OnPropertChanged("BarCodeType"); }
            }
            private string _barCodeText;
            public string BarCodeText
            {
                get { return _barCodeText; }
                set { _barCodeText = value;OnPropertChanged("BarCodeText"); }
            }
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value;OnPropertChanged("Name"); }
            }
            private string _organization;
            public string Organization
            {
                get { return _organization; }
                set { _organization = value;OnPropertChanged("Organization"); }
            }
            private string _address;
            public string Address
            {
                get { return _address; }
                set { _address = value;OnPropertChanged("Address"); }
            }
            private string _zip;
            public string Zip
            {
                get { return _zip; }
                set { _zip = value;OnPropertChanged("Zip"); }
            }
            private string _country;
            public string Country
            {
                get { return _country; }
                set { _country = value;OnPropertChanged("Country"); }
            }
            private string my_homePhone;
            public string HomePhone
            {
                get { return my_homePhone; }
                set { my_homePhone = value;OnPropertChanged("HomePhone"); }
            }
            private string _fax;
            public string Fax
            {
                get { return _fax; }
                set { _fax = value;OnPropertChanged("Fax"); }
            }
            private string _resultantCard;
            public string ResultantCard
            {
                get { return "Name:" + Name + "Organization:" + Organization + "Address:" + Address + "Zip:" + Zip + "Country:" + Country + "HomePhone:" + HomePhone + "Fax:" + Fax; }
                set
                {
                    _resultantCard = value;
                    OnPropertChanged("ResultantCard");
                }
            }
            private bool _isBarCodeTypeEnabled;
            public bool IsBarCodeTypeEnabled
            {
                get { return _isBarCodeTypeEnabled; }
                set { _isBarCodeTypeEnabled = value;OnPropertChanged("IsBarCodeTypeEnabled"); }
            }
            private bool _exportButtonVisibility;
            public bool ExportButtonVisibility
            {
                get { return _exportButtonVisibility; }
                set { _exportButtonVisibility = value;OnPropertChanged("ExportButtonVisibility"); }
            }
            #endregion Properties
            //Icommand for button used for regenerating the BarCode
            #region ICommand
            private ICommand _regenerateCode = null;
            public ICommand RegenerateCode
            {
                get
                {
                    _regenerateCode = new RelayCommand(RegenerateBarCode);
                    return _regenerateCode;
                }
                set { _regenerateCode = value; }
            }
            #endregion ICommand
            //Method called by Icommand to regerate the BarCode
            private void RegenerateBarCode(object obj)
            {
                counter += 1;
                CreateBarCode();
            }
            //Constructor
            #region Constructor
            public BarCodeViewModel()
            {
                //Initializng all the lists and populating them with Data
                //Barcodes = new List<CodeType>() { CodeType.Bc412, CodeType.Code11, CodeType.HIBCCode128, CodeType.HIBCCode39, CodeType.Iata25, CodeType.IntelligentMailPackage, CodeType.ISBN, CodeType.ISMN, CodeType.ISSN, CodeType.ITF14, CodeType.MicroQRCode, CodeType.Pharmacode, CodeType.Plessey, CodeType.PZN, CodeType.SSCC18, CodeType.Telepen,CodeType.QRCode,CodeType.DataMatrix,CodeType.Pdf417,CodeType.Code39x };
                Categories = new List<string>() { "Email", "Text", "Url", "VfCard" };
                IsBarCodeTypeEnabled = false;
                ExportButtonVisibility = false;
            }
            #endregion Constructor
            //Method to check which options are selected in ComboBox and create BarCode accordingly
            public void CreateBarCode()
            {
                if(BarcodesChanged != null && CategoriesChanged!=null)
                {
                    ExportButtonVisibility = true;
                    var codetype = BarcodesChanged;
                    switch (codetype)
                    {
                        case "HIBCCode128":
                            BarCodeType = CodeType.HIBCCode128;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "A123PROD78905/0123456789DATA";
                                BarCodeText = Text;
                            } 
                            break;
                        case "HIBCCode39":
                            BarCodeType = CodeType.HIBCCode39;
                            if(counter==1) { BarCodeText= Text;counter=0; }
                            else
                            {
                                Text = @"A123PROD78905/0123456789DATA";
                                BarCodeText = Text;
                            }
                            break;
                        case "IntelligentMailPackage":
                            BarCodeType = CodeType.IntelligentMailPackage;
                            if(counter==1) { BarCodeText= Text;counter = 0; }
                            else
                            {
                                Text = "9212391234567812345671";
                                BarCodeText = Text;
                            }
                            break;
                        case "ISBN":
                            BarCodeType = CodeType.ISBN;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "9790123456785";
                                BarCodeText = Text;
                            }
                            break;
                        case "ISMN":
                            BarCodeType = CodeType.ISMN;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "9790123456785";
                                BarCodeText = Text;
                            }
                            break;
                        case "ISSN":
                            BarCodeType = CodeType.ISSN;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "9790123456785";
                                BarCodeText = Text;
                            }
                            break;
                        case "PZN":
                            BarCodeType = CodeType.PZN;
                            if(counter==1) { BarCodeText= Text;counter = 0; }
                            else
                            {
                                Text = "01234562";
                                BarCodeText = Text;
                            }
                            break;
                        case "Pharmacode":
                            BarCodeType = CodeType.Pharmacode;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "131070";
                                BarCodeText = Text;
                            }
                            break;
                        case "Plessey":
                            BarCodeType = CodeType.Plessey;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "9790123456785";
                                BarCodeText = Text;
                            }
                            break;
                        case "SSCC18":
                            BarCodeType = CodeType.SSCC18;
                            if(counter==1) { BarCodeText = Text;counter = 0; }
                            else
                            {
                                Text = "1234t5+678912345678";
                                BarCodeText = Text;
                            }
                            break;
                        case "Telepen":
                            BarCodeType = CodeType.Telepen;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "9790123456785";
                                BarCodeText = Text;
                            }
                            break;
                        case "Bc412":
                            BarCodeType = CodeType.Bc412;
                            if(counter == 1) { BarCodeText = Text;counter = 0; }
                            else
                            {
                                Text = "AQ1557";
                                BarCodeText = Text;
                            }
                            break;
                        case "Code11":
                            BarCodeType = CodeType.Code11;
                            if (counter == 1) { BarCodeText = Text; counter = 0; }
                            else
                            {
                                Text = "9790123456785";
                                BarCodeText = Text;
                            }
                            break;
                        case "MicroQRCode":
                            BarCodeType = CodeType.MicroQRCode;
                            if(counter==1) { BarCodeText = Text;counter = 0; }
                            else
                            {
                                Text = "12345";
                                BarCodeText = Text;
                            }
                            break;
                        case "Iata25":
                            BarCodeType = CodeType.Iata25;
                            if(counter==1) { BarCodeText = Text;counter = 0; }
                            else
                            {
                                Text = "0123456789";
                                BarCodeText = Text;
                            }
                            break;
                        case "ITF14":
                            BarCodeType = CodeType.ITF14;
                            if (counter == 1) { BarCodeText = Text;counter = 0; }
                            else
                            {
                                Text = "1234567891011";
                                BarCodeText = Text;
                            }
                            break;
                        case "QRCode":
                           
                            BarCodeType = CodeType.QRCode;
                            if (CategoriesChanged == "Text")
                            {
                                if (counter == 1) { BarCodeText = Text;counter=0; }
                                else
                                {
                                    Text = "Welcome to use FlexGrid control";
                                    BarCodeText = Text;
                                }
                            }
                            else if (CategoriesChanged == "Url")
                            {
                                if(counter == 1) { BarCodeText = Url;counter=0;}
                                else
                                {
                                    Url = "https://www.grapecity.com.cn/componentone";
                                    BarCodeText = Url;
                                }
                            }
                            else if (CategoriesChanged == "Email")
                            {
                                if(counter==1) { BarCodeText = Email; counter=0; }
                                else
                                {
                                    Email = "us.sales@mescius.com";
                                    BarCodeText = Email;
                                }
                            }
                            else
                            {
                                if (counter == 1) { BarCodeText = ResultantCard; counter = 0; }
                                else
                                {
                                    Name = "Mr.C1.Sales.Jr";
                                    Organization = "MESCIUS";
                                    Address = "Noida";
                                    Zip = "11012";
                                    Country = "India";
                                    HomePhone = "987652232";
                                    Fax = "102345";
                                    BarCodeText = ResultantCard;
                                }
                            }
                            break;
                        case "DataMatrix":
                            BarCodeType = CodeType.DataMatrix;
                            if (CategoriesChanged == "Text")
                            {
                                if(counter == 1) { BarCodeText = Text; counter=0;}
                                else
                                {
                                    Text = "Welcome to use FlexGrid control";
                                    BarCodeText = Text;
                                }
                            }
                            else if (CategoriesChanged == "Url")
                            {
                                if (counter == 1) { BarCodeText = Url;counter=0;}
                                else
                                {
                                    Url = "https://developer.mescius.com/componentone";
                                    BarCodeText = Url;
                                }
                            }
                            else if (CategoriesChanged == "Email")
                            {
                                if (counter == 1) { BarCodeText = Email; counter=0;}
                                else
                                {
                                    Email = "us.sales@mescius.com";
                                    BarCodeText = Email;
                                }
                            }
                            else
                            {
                                if (counter == 1) { BarCodeText = ResultantCard; counter = 0; }
                                else
                                {
                                    Name = "Mr.C1.Sales.Jr";
                                    Organization = "MESCIUS";
                                    Address = "Noida";
                                    Zip = "11012";
                                    Country = "India";
                                    HomePhone = "987652232";
                                    Fax = "102345";
                                    BarCodeText = ResultantCard;
                                }
                            }
                            break;
                        case "Pdf417":
                            BarCodeType = CodeType.Pdf417;
                            if (CategoriesChanged == "Text")
                            {
                                if(counter==1) { BarCodeText = Text;counter = 0; }
                                else
                                {
                                    Text = "Welcome to use FlexGrid control";
                                    BarCodeText = Text;
                                }
                            }
                            else if (CategoriesChanged == "Url")
                            {
                                if (counter == 1) { BarCodeText = Url;counter=0;}
                                else
                                {
                                    Url = "https://developer.mescius.com/componentone";
                                    BarCodeText = Url;
                                }
                                   
                            }
                            else if (CategoriesChanged == "Email")
                            {
                                if (counter == 1) { BarCodeText = Email;counter=0;}
                                else
                                {
                                    Email = "us.sales@mescius.com";
                                    BarCodeText = Email;
                                }
                            }
                            else
                            {
                                if (counter == 1) { BarCodeText = ResultantCard; counter = 0; }
                                else
                                {
                                    Name = "Mr.C1.Sales.Jr";
                                    Organization = "MESCIUS";
                                    Address = "Noida";
                                    Zip = "11012";
                                    Country = "India";
                                    HomePhone = "987652232";
                                    Fax = "102345";
                                    BarCodeText = ResultantCard;
                                }
                            }
                            break;
                        case "Code39x":
                            BarCodeType = CodeType.Code39x;
                            if (CategoriesChanged == "Text")
                            {
                                if (counter == 1) { BarCodeText= Text;counter=0;}
                                else
                                {
                                    Text = "Welcome to use FlexGrid control";
                                    BarCodeText = Text;
                                }
                            }
                            else if (CategoriesChanged == "Url")
                            {
                                if(counter==1) { BarCodeText= Url; counter=0;}
                                else
                                {
                                    Url = "https://developer.mescius.com/componentone";
                                    BarCodeText = Url;
                                }
                            }
                            else if (CategoriesChanged == "Email")
                            {
                                if (counter == 1) { BarCodeText= Email;counter=0;}
                                else
                                {
                                    Email = "us.sales@mescius.com";
                                    BarCodeText = Email;
                                }
                            }
                            else
                            {
                                if (counter == 1) { BarCodeText = ResultantCard; counter = 0; }
                                else
                                {
                                    Name = "Mr.C1.Sales.Jr";
                                    Organization = "MESCIUS";
                                    Address = "Noida";
                                    Zip = "11012";
                                    Country = "India";
                                    HomePhone = "987652232";
                                    Fax = "102345";
                                    BarCodeText = ResultantCard;
                                }
                            }
                            break;
                    }
                }
            }
        }
        //Class handling INotifyChanges
        #region INotifyPropertyChanges
        public class ViewModelBase : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            public void OnPropertChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    }
    #endregion INotifyPropertyChanges