C# DataBinding - automatically writing changed property to label or textbox
Solution 1
Another way to make things work without implementing INotifyPropertyChanged
class Class1
{
private string name;
public string Name
{
get { return name; }
set
{
//Check if you are assigning the same value. Depends on your business logic
//here is the simplest check
if (Equals(name, value))
return;
name = value;
OnNameChanged();
}
public event EventHandler NameChanged;
protected virtual void OnNameChanged()
{
var handler = NameChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
}
The trick is to have event with the name combined by name of property and Changed
suffix and to raise it whenever value of your property is changed
Solution 2
In order your code would work you should implement INotifyPropertyChanged interface in your binded class. Without it your binding simply doesn't know, when the change occures. There you should implenent the logic, according to which you would notify your subscribers about which when something changed in your class (the setter part) and what has changed (PropertyChangedEventArgs). See example for your class:
class Class1: INotifyPropertyChanged
{
private string name = "";
public string Name
{
get { return name; }
set { name = value; NotifyPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged()
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
}
And change the property name from "name" to "Name" in your binding:
label1.DataBindings.Add(new Binding("Text", bs, "Name", false, DataSourceUpdateMode.OnPropertyChanged));
Patrick
Updated on June 04, 2022Comments
-
Patrick about 2 years
I read some about DataBinding, mostly complicated things like SQL or whatever XAML and stuff. All I want my programm to do is, if the "value" of a variable changes just write it in a textbox or label. (using WindowsForms)
So far what I have:
namespace DataBinding_Test { public partial class Form1 : Form { BindingSource bs = new BindingSource(); Class1 test = new Class1(); public Form1() { InitializeComponent(); test.name = "Hello"; bs.DataSource = test; label1.DataBindings.Add(new Binding("Text", bs, "name", false, DataSourceUpdateMode.OnPropertyChanged)); } private void button1_Click(object sender, EventArgs e) { test.name = textBox1.Text; } } }
Class1 just has a public property name. On startup lable1 will show my "Hello" string. Then on button click the name property will change. On debug I saw the actual DataSource of "bs" contains the new property value, but the label will not show anything...
Is there any realtivly easy way to do this?
The Backround is: periodically there will be a polling of sensor data throug RS232. If the value of one sensor changes I want to show this in label or textbox. Now a backroundthreaded timer will need invokes and stuff to access the GUI thread; thought this would be easier with databinding but seems not :P
Thanks to all, great site, great work! :)
-
default almost 11 yearsYou should work on your titles. Tags are not neccessary which means
C#
anddatabindings
can be removed which leaves "is there an easy way?" where the usual answer is "yes. yes there is". The title should explain in short what your problem is. -
Patrick almost 11 yearsDone, hopefully it is better now!
-
default almost 11 yearsHave you read about ´event`s? I guess you could use that here. Have you tried using WPF instead? Databindings are more custom in WPF. I don't think there is any magical "automatic" way to do this
-
Patrick almost 11 yearsYes I know events; is there a way to implement propertychanged events by myself?
-
-
Patrick almost 11 yearsThe Backround is: periodically there will be a polling of sensor data throug RS232. If the value of one sensor changes I want to show this in label or textbox. Now a backroundthreaded timer will need invokes and stuff to access the GUI thread; thought this would be easier with databinding but seems not :P (will add the backround in the actual question to be more clear)
-
Demarsch almost 11 yearsDon't forget to check if new value differs from the old one
-
Alex almost 11 years@colosso It is not always easy way to be the way to go. Binding is a very powerful feature, which makes your application more managed, better test-driven and developer-independent. I argee that your answer is the soulution to the problem, but suppose in real projects it wont be acceptable.
-
Alex almost 11 yearsYes, this should work also, be I suppose, that it is better to use INotifyPropertyChanged, because it is a standard, and when another developer sees it implemented, he understands, that the class supports bindings logic.
-
colosso almost 11 years@voo Thanks for your comment. I agree with you. It's a simple solution but at some points for sure not the best way to go. It really depends on the whole background which way is better to go. In my oppinion the invoking solution is pretty good. I've made very good experiences using it in threads collaborating with WinForms.