Click here to Skip to main content
16,015,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai friends,

Can anybody explain me how to work with TwoWay Binding. For Example,

I have 2 textboxs with property Twoway Binding, i need to insert the data from those 2 textboxes to my Database (or) to XmlFile. I came to know that we have to use the class called DataContext and Notifychanded property sth..., but i dont know how to use those. It will be helpful to explain with some sample code.

Thanks.....
Posted

You need to folow below steps,

Your binding class should be implemeted with INotifyPropertyChanged Interface where you need to implement relavant properties, as below example.

C#
public class MyProduct : INotifyPropertyChanged
    {
        private Dictionary<string, object> _properties = new Dictionary<string, object>();

        protected virtual T GetValue<T>(string name)
        {
            if (this._properties.ContainsKey(name))
                return (T)this._properties[name];

            return default(T);
        }

        protected virtual void SetValue<T>(string name, T value)
        {
            bool set = false;
            if (!this._properties.ContainsKey(name))
            {
                this._properties.Add(name, value);
                set = true;
            }
            else if (!object.Equals(this._properties[name], value))
            {
                this._properties[name] = value;
                set = true;
            }
            if (set)
            {
                this.NotifyChange(name);
            }
        }

        protected virtual void NotifyChange(params string[] properties)
        {
            if (PropertyChanged != null)
            {
                foreach (string p in properties)
                    PropertyChanged(this, new PropertyChangedEventArgs(p));
            }
        }
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }





Then this class need to bind with relavnt gird and property with relavant textbox with two way binding mode.

C++
Mode="TwoWay"



Thanks,
Amit Patel
 
Share this answer
 
Hai Amit M Patel, ur code is fine, but can u pls explain how to send the data from textbox to the Product Class properties to(SetValue & GetValue) and how the data can be stored to database. Better to have some clear understanding about this and i will try my own with ur example.
 
Share this answer
 
Comments
Abhinav S 25-Jul-10 5:19am    
For storing data in a database you will need a web service.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900