[solved] Two-way binding winforms checkbox to property?
-
I have this code
public partial class TrimOptions : Form
{
public bool trimFirst { get; set; } = false;
public bool trimLast { get; set; } = false;
public bool ifBlank { get; set; } = true;public TrimOptions() { InitializeComponent(); chkTrimFirst.DataBindings.Add(new Binding("Checked", trimFirst, null, false, DataSourceUpdateMode.OnPropertyChanged)); chkTrimLast.DataBindings.Add(new Binding("Checked", trimLast, null, false, DataSourceUpdateMode.OnPropertyChanged)); chkIfBlank.DataBindings.Add(new Binding("Checked", ifBlank, null, false, DataSourceUpdateMode.OnPropertyChanged)); }
Whatever I set the properties to in code is reflected in the UI when the form is displayed. So far, so good. However, the user clicking the checkboxes does not set the properties, and setting the properties programmatically does not change the UI (nor the Checked property of the control). I would like programmatic changes to the properties to update the UI, and I would like the user changing the UI to update the properties. How to do that?
-
I have this code
public partial class TrimOptions : Form
{
public bool trimFirst { get; set; } = false;
public bool trimLast { get; set; } = false;
public bool ifBlank { get; set; } = true;public TrimOptions() { InitializeComponent(); chkTrimFirst.DataBindings.Add(new Binding("Checked", trimFirst, null, false, DataSourceUpdateMode.OnPropertyChanged)); chkTrimLast.DataBindings.Add(new Binding("Checked", trimLast, null, false, DataSourceUpdateMode.OnPropertyChanged)); chkIfBlank.DataBindings.Add(new Binding("Checked", ifBlank, null, false, DataSourceUpdateMode.OnPropertyChanged)); }
Whatever I set the properties to in code is reflected in the UI when the form is displayed. So far, so good. However, the user clicking the checkboxes does not set the properties, and setting the properties programmatically does not change the UI (nor the Checked property of the control). I would like programmatic changes to the properties to update the UI, and I would like the user changing the UI to update the properties. How to do that?
You're confusing "data sources" and "data members". [Binding Constructor (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.-ctor?view=net-5.0#System\_Windows\_Forms\_Binding\_\_ctor\_System\_String\_System\_Object\_System\_String\_)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
You're confusing "data sources" and "data members". [Binding Constructor (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.-ctor?view=net-5.0#System\_Windows\_Forms\_Binding\_\_ctor\_System\_String\_System\_Object\_System\_String\_)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
Thank you!
chkTrimFirst.DataBindings.Add(new Binding("Checked", this, "trimFirst", false, DataSourceUpdateMode.OnPropertyChanged));
chkTrimLast.DataBindings.Add(new Binding("Checked", this, "trimLast", false, DataSourceUpdateMode.OnPropertyChanged));
chkIfBlank.DataBindings.Add(new Binding("Checked", this, "ifBlank", false, DataSourceUpdateMode.OnPropertyChanged));This is working just fine now.
-
Thank you!
chkTrimFirst.DataBindings.Add(new Binding("Checked", this, "trimFirst", false, DataSourceUpdateMode.OnPropertyChanged));
chkTrimLast.DataBindings.Add(new Binding("Checked", this, "trimLast", false, DataSourceUpdateMode.OnPropertyChanged));
chkIfBlank.DataBindings.Add(new Binding("Checked", this, "ifBlank", false, DataSourceUpdateMode.OnPropertyChanged));This is working just fine now.