In C#, you can bind a property of an object to a user interface element using data binding.
Here's an example of how to bind a TextBox control to the Text property of a Person object:
using System.ComponentModel; public class Person : INotifyPropertyChanged { private string _name; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } // Create a new Person object and set the Name property Person person = new Person { Name = "John" }; // Bind the Text property of a TextBox control to the Name property of the Person object textBox.DataBindings.Add(new Binding("Text", person, "Name", false, DataSourceUpdateMode.OnPropertyChanged)); In this example, a Person object is created with a Name property that raises a PropertyChanged event when changed. The TextBox control is bound to the Name property of the Person object using the Binding constructor. The first argument specifies the name of the property to bind to ("Text" for the TextBox control's Text property), the second argument specifies the object to bind to (person in this case), the third argument specifies the name of the property of the object to bind to ("Name" in this case), and the last argument specifies the DataSourceUpdateMode, which determines when the data should be updated (in this case, DataSourceUpdateMode.OnPropertyChanged specifies that the data should be updated whenever the PropertyChanged event is raised).
When the user types into the TextBox control, the Name property of the Person object is updated, and the PropertyChanged event is raised. This causes the TextBox control to update its Text property to reflect the new value of the Name property.
Note that the Person class implements the INotifyPropertyChanged interface, which is required for data binding to work properly. The OnPropertyChanged method raises the PropertyChanged event, which notifies the user interface that the value of a property has changed.
"C# Data binding to a TextBox property"
textBox.DataBindings.Add("Text", myObject, "PropertyName"); Text property of a TextBox to the "PropertyName" property of myObject."C# Two-way data binding to a ListBox"
listBox.DataBindings.Add("SelectedItem", myObject, "PropertyName", true, DataSourceUpdateMode.OnPropertyChanged); SelectedItem property of a ListBox and the "PropertyName" property of myObject."C# Binding to a property with formatting"
label.DataBindings.Add("Text", myObject, "NumericProperty", true, DataSourceUpdateMode.Never, "", "N2"); Text property of a Label to a numeric property with formatting options."C# Conditional data binding with BindingSource"
BindingSource bindingSource = new BindingSource(); bindingSource.DataSource = myObject; label.DataBindings.Add("Text", bindingSource, "PropertyName", true, DataSourceUpdateMode.OnPropertyChanged); BindingSource for conditional data binding with the "PropertyName" property of myObject."C# Data binding to a DataGridView"
dataGridView.DataSource = myList; // List of objects dataGridView.Columns["PropertyName"].DataPropertyName = "PropertyName";
"C# Binding to a ComboBox with DisplayMember and ValueMember"
comboBox.DataSource = myList; // List of objects comboBox.DisplayMember = "DisplayName"; comboBox.ValueMember = "ID";
"C# Binding to a property with custom formatting"
textBox.DataBindings.Add("Text", myObject, "DateProperty", true, DataSourceUpdateMode.Never, "", "dd/MM/yyyy"); Text property of a TextBox to a date property with custom formatting."C# Binding to a property with null value handling"
textBox.DataBindings.Add("Text", myObject, "NullableProperty", true, DataSourceUpdateMode.OnPropertyChanged, string.Empty, null); Text property of a TextBox to a nullable property."C# Binding to a property with event handlers"
Binding binding = new Binding("Text", myObject, "PropertyName", true, DataSourceUpdateMode.OnPropertyChanged); binding.Parse += (sender, args) => { /* custom parse logic */ }; textBox.DataBindings.Add(binding); "C# Dynamic data binding in WPF"
var binding = new Binding("PropertyName") { Source = myObject }; myControl.SetBinding(Control.Property, binding); camera handlebars.js redux-saga microsoft-dynamics controlvalueaccessor backwards-compatibility azure-log-analytics flutter-streambuilder maxlength asp.net-mvc-3