Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding property to control in Winforms

What is the best way to bind a property to a control so that when the property value is changed, the control's bound property changes with it.

So if I have a property FirstName which I want to bind to a textbox's txtFirstName text value. So if I change FirstName to value "Stack" then the property txtFirstName.Text also changes to value "Stack".

I know this may sound a stupid question but I'll appreciate the help.

like image 938
cubski Avatar asked May 04 '11 12:05

cubski


People also ask

What is Data Binding in WinForms?

In this article Then, the Visual Studio tools are used to bind the types defined in the model to the WinForms controls. The WinForms data-binding framework enables navigation between related objects: selecting rows in the master view causes the detail view to update with the corresponding child data.

What is Property binding in C#?

The Binding class is used to bind a property of a control with the property of an object. For creating a Binding object, the developer must specify the property of the control, the data source, and the table field to which the given property will be bound.


2 Answers

A simplified version of the accepted answer that does NOT require you to type names of properties manually in every property setter like OnPropertyChanged("some-property-name"). Instead you just call OnPropertyChanged() without parameters:

You need .NET 4.5 minimum. CallerMemberName is in the System.Runtime.CompilerServices namespace

public class Sample : INotifyPropertyChanged {     private string _propString;     private int _propInt;       //======================================     // Actual implementation     //======================================     public event PropertyChangedEventHandler PropertyChanged;      [NotifyPropertyChangedInvocator]     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)     {         var handler = PropertyChanged;         if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));     }     //======================================     // END: actual implementation     //======================================       public string PropString     {         get { return _propString; }         set         {             // do not trigger change event if values are the same             if (Equals(value, _propString)) return;             _propString = value;              //===================             // Usage in the Source             //===================             OnPropertyChanged();          }     }      public int PropInt     {         get { return _propInt; }         set         {             // do not allow negative numbers, but always trigger a change event             _propInt = value < 0 ? 0 : value;             OnPropertyChanged();         }     } } 

Usage stays the same:

var source = new Sample(); textbox.DataBindings.Add("Text", source, "PropString"); source.PropString = "Some new string"; 

Hope this helps someone.

like image 41
Dmitry Avtonomov Avatar answered Sep 21 '22 14:09

Dmitry Avtonomov


You must implement INotifyPropertyChanged And add binding to textbox.

I will provide C# code snippet. Hope it helps

class Sample : INotifyPropertyChanged {     private string firstName;     public string FirstName     {         get { return firstName; }         set         {             firstName = value;             InvokePropertyChanged(new PropertyChangedEventArgs("FirstName"));         }     }      #region Implementation of INotifyPropertyChanged      public event PropertyChangedEventHandler PropertyChanged;      public void InvokePropertyChanged(PropertyChangedEventArgs e)     {         PropertyChangedEventHandler handler = PropertyChanged;         if (handler != null) handler(this, e);     }      #endregion } 

Usage :

 Sample sourceObject = new Sample();  textbox.DataBindings.Add("Text",sourceObject,"FirstName");  sourceObject.FirstName = "Stack";  
like image 90
Stecya Avatar answered Sep 21 '22 14:09

Stecya