One-Way DataBindings?

  • Thread starter Thread starter Andreas Zita
  • Start date Start date
A

Andreas Zita

Hi,

I have a form with two controls, one button and one textbox. At form load I
add a databinding between the text property of the textbox and a public
property MyValue of the form which gets and sets a string field. The string
is initiated with the text "default" which also is what the textbox
initially shows after the databinding has been added. When editing the
textfield (and after tabbing out) the string field is updated as it should.
However, I would also like the textbox string to change whenever the MyValue
property is set from elsewhere in the form without having to set it
manually. Shouldnt this be done automatically since I have added a
databinding between these two properties or is a databinding "one-way" only?

/Andreas
 
Hi,

I have a form with two controls, one button and one textbox. At form load I
add a databinding between the text property of the textbox and a public
property MyValue of the form which gets and sets a string field. The string
is initiated with the text "default" which also is what the textbox
initially shows after the databinding has been added. When editing the
textfield (and after tabbing out) the string field is updated as it should.
However, I would also like the textbox string to change whenever the MyValue
property is set from elsewhere in the form without having to set it
manually. Shouldnt this be done automatically since I have added a
databinding between these two properties or is a databinding "one-way" only?

/Andreas


Dear Andreas

In order to get the data binding "infrastructure" to recognise the
change in value of your form's property (MyValue) as soon as possible
you can implement a "changed event"...

public event EventHandler MyValueChanged;

.... note that the name should be the name of your property plus the
word "Changed".

Then in your property's set accessor you check for subscribers and
"raise the event"...

public string MyValue
{
get
{
return myValue;
}
set
{
myValue = value;
if (MyValueChanged != null)
{
MyValueChanged(this,System.EventArgs.Empty);
}
}
}

// Private field for property...
string myValue = "Default value";


You'll find that the data binding "infrastructure" has subscribed to
your event will update any properties "bound" to your form's MyValue
property.

Hope this helps

Regards

Neil Allen
 
Back
Top