How to Property binding in C#


I love the data binding features rather than anything else in my visual studio. I am going to demonstrate binding feature in c#.

We have an MDI form called ‘Master’ and a Label on it and another child form in My C# Project. The child simply shows ‘company’ with ‘codes’.

Task: Want to display Company name on Master when the user double-click the company.

How to, do this, we can achieve this with global variables, maybe, but need to refresh the label every time. But the binding feature offers the better solution for this.

  1. We need to create the property to access label control  Text in master as follows
private string foo="My Company";
 public string Company
 { 
 
 set{

foo=value;
 
 }
 get { return foo; }
 }

Now place the label data binding code just below the Initialize section of the ‘Master’.

InitializeComponent();
lbl_company.DataBindings.Add("Text", this, "Company");

The code will work every time when the label Text changes. Actually, we need to change the value of the label when company property altered, which require a two-way binding. This is possible with the INotifyPropertyChanged interface.

Property Binding with Control -c#

How to data bind controls in C#

Our Master class need to be modified as follows

 public partial class Master: Form,INotifyPropertyChanged{

 public Master()
 {
 InitializeComponent();
 lbl_company.DataBindings.Add("Text", this, "Company");
 }

 private void pictureBox16_Click(object sender, EventArgs e)
 {
 ChildForm c = new ChildForm (); c.MdiParent = this;
 ac.Show(); 
 
 }

private string foo;
 public string Company
 { 
 
 set{

foo=value;
 OnPropertyChanged("Company");
 }
 get { return foo; }
 }
 protected virtual void OnPropertyChanged(string property)
 {
 if (PropertyChanged != null)
 PropertyChanged(this, new PropertyChangedEventArgs(property));
 }

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;
 #endregion
 }
}

2. Remaining code will reside on the child form. We need to access the parent and then the parent’s company property as follows. Drop the code on command click  event

 string code, cname, path;
 cname = listView1.SelectedItems[0].SubItems[1].Text.ToString(); 
 Master p1 = (Master )this.Parent; 
 p1.Company = cname;

Whenever the user clicks the button with a different company name the label on the Master will automatically update with company name selected.

This is just an example of how data binding works, you can innovate another application of binding.

See the complete source code on GitHub

All of my gits here 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.