less than 1 minute read

You used DataBinding to bind a property to an object but even though state has been changed, your object is not updated. In my case I have a CheckBox and I bound it to another class’ variable. Settings is the other class and Form1 is the main GUI.

In the following example you will see how to bind a CheckBox to a bool variable. Further, that variable resides in another Class instance. By adding DataSourceUpdateMode update mode is changed to OnPropertyChanged. In the end, property is changed at each change in the CheckBox.

public class Settings
{
	private bool showid = true;
	public bool showID
	{
		get
		{
			return showid;
		}
		set
		{
			showid = value;
		}
	}
}

public class Form1
{
	private CheckBox ShowID;
	public Form1()
	{
		Settings mySettings = new Settings();
		this.ShowID.DataBindings.Add("Checked", mySettings, "showID");
		this.ShowID.DataBindings[0].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
	}
}

Categories:

Updated:

Comments