Click here to Skip to main content
14,608,021 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a Problem with my app. I build an app with WPF. I have a data it is imported from DB with observablecollection, part of this data (one properity) is string and I want to bind this data to checkboxes this string data Looks like
(abc_abcd/abc_abcd/abc_abcd.....)
every abc_abcd it should be linked to one Checkbox( this string is user authorisations and it stored in the data base as one string but when i want to bind it to checkboxes i should spilt it and then compare it with static value for every Checkbox to know if tjis Checkbox should be checked or not ). I know that I should use ivalueconverter but I don't have an idea how to make it. any Solution please......


class Operator
    {
        public string operatorname { get ; set; }
        public string  operatorpassword { get; set; }
        public string authorisation { get; set; } 
        public Boolean renew_password { get; set; }
        public int interval_of_change { get; set; }
        public int password_min_length { get; set; }
        public DateTime last_change { get; set; }
    }


 class Databasecon  
    {
       

       

        // First Binding for the Database
       public ObservableCollection<Operator> operators { get; private set; }


        public Databasecon()
        {

            this.operators = new ObservableCollection<Operator>();
            
        }
       
       public void Datacon(string conn)
        {
            MySqlConnection con = null;
            MySqlCommand com = null;
            MySqlDataReader myreader = null;
            int columnOrdinaloperatorname = -1;
            int columnOrdinalauthorisation = -1;


            con = new MySqlConnection(conn);
            try
            {
                if (com == null)
                {
                    com = new MySqlCommand("SELECT * FROM operators", con);
                    com.Connection.Open();

                    myreader = com.ExecuteReader();
                    columnOrdinaloperatorname = myreader.GetOrdinal("operator_name");
                    columnOrdinalauthorisation = myreader.GetOrdinal("authorisation");

                    while (myreader.Read())
                    {

                        this.operators.Add(new Operator() {
                            operatorname = myreader.GetString(columnOrdinaloperatorname).ToString(),
                             // authorisation can be Null in DB
                            authorisation = (columnOrdinalauthorisation >= 0) ? (myreader.IsDBNull(columnOrdinalauthorisation) ? string.Empty : myreader.GetString(columnOrdinalauthorisation)) : string.Empty
                        }); 

                    }

                    
                    
                }


            }


            catch (MySqlException ex)
            {
                MessageBox.Show(ex.ToString());
               

            }
            finally
            {

                if (myreader != null)
                    myreader.Close();

                if (com != null)
                {
                    if (com.Connection != null)
                        com.Connection.Close();
                }
            
            }
    }
   
}

pre>


<pre lang="c#"></





class collect : INotifyPropertyChanged

    {

        string myconn = "SERVER=localhost;PORT=3306;UID=TestUser;PASSWORD=TestUser;DATABASE=heitel_db_v011";

        private Databasecon databasecon = null;

        public event PropertyChangedEventHandler PropertyChanged;

        public ICommand Bindingcommand { get; set; }





        public ObservableCollection<Operator> operators
        {
            get
            {
                if (this.databasecon.operators != null)
                   
                {
                       return this.databasecon.operators;
                   
                      
                }

                else
                {


                    return null;
                }
                   

            }
            set
            {
                this.operators = value; RaisePropertyChanged("operators");
            }

        }

        private void RaisePropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }


        public collect()
        { 
            this.databasecon = new Databasecon();
            Bindingcommand = new Relaycommands(() => databasecon.Datacon(myconn));
           
        }


    }


class Relaycommands : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private Action _command;


        public Relaycommands(Action command)
        {
            _command = command;

        }
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            if (_command != null)
                _command();
        }
    }


What I have tried:

I try to write the converter but I couldn't. I don't know when I should Spill the string data to words and save it in an Array
Posted
Updated 12-Apr-17 3:25am
v4
Comments
[no name] 12-Apr-17 8:51am
   
"but I couldn't", and you think we would know what that means? Possibly because strings are not checkboxes?
"when I should Spill the string data to words", "save it in an Array", the major mistake you are making that you you assume that random strangers on the internet know anything about your project. We have noidea when or why you would need to split this string or what an array has to do with anything. We also would have no idea what this Operator class has to do with anything since it apparently has nothing at all to do with any value converter.
Ahmad Aldali 12-Apr-17 9:07am
   
sorry, i edit my quistion and explain every Thing and put the whole code

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100