Hmm... I must be dense or something when it comes to understanding the databinding in WinForms. I think I’m misunderstanding the concept…
Here’s what I want to do:
I have a created a very simple form and a very simple POCO object class called address like this:
public partial class Form1 : Form
{
protected Address MyAddress = null;
public Form1()
{
MyAddress = new Address();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(this.MyAddress.Name);
}
}
public class Address
{
public string Name
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
string _Name = "Rick Strahl";
public string Company
{
get
{
return _Company;
}
set
{
_Company = value;
}
}
string _Company = "West WindowsApplication1";
public string StreetAddress
{
get
{
return _Address;
}
set
{
_Address = value;
}
}
string _Address = "32 Kaiea";
public DateTime Entered
{
get
{
return _Entered;
}
set
{
_Entered = value;
}
}
DateTime _Entered = DateTime.Now;
}
The idea is that I want to bind the Address object and the name property thereof to the textbox of the form.
So I do the following:
- Add a new BindingSource object to the form called Binder
- Set the datasource by creating a new DataSource and pointing it at the Address object
- Go into the textbox, select databindings, pick Binder and Select the Address.Name property
(it shows Binder – Name at this point for DataBindings)
I run the form…
… and nothing. No databinding.
I was under the impression that the above is all you should have to do for binding – you implicitly set up the binding and once set the two are kept in sync. What am I missing here?
I took a quick look at the interface on the binder and it doesn’t look like there’s anything on that interface that is used to tell it to ‘start binding’.
So what am I missing here?
I actually ran into this about a week back, posted a message and got some vague references to a number of databinding articles that talk about database binding which obviously works differently. I also talked to a couple of people in the course of the week and they too said this should 'just' work as is.
Hmmm... I suspect I'm either not calling something that is non-obvious, or the object needs to implement a special interface or something like that. However, I'd expect the IDE to not let me bind to it if it wasn't going to work...
Feel free to tell me to RTFM, but I searched around for a while back then and couldn't find a good article describing object binding in a scenario like the above.
<sigh>. Obviously this is not quite as obvious as it seems.
Some first thoughts
BTW, my first impression of this approach is that this is all very badly laid out. While an improvement over 1.x default binding behavior this process still involves way too many steps (even if it actually worked) in the IDE. The DataBindings property is even collapsed so you can’t directly go there – if you are binding this is one of the most used properties you will be setting. In addition it looks like you’ll need one binding source for each data source you’re binding to on a form, which can get messy quickly if you have a few datasources you need to bind to.
I personally prefer direct connectivity between data source and the control with properties on the control – it’s just so much more efficient to work with. The one advantage with the binder is that you can isolate binding related logic as the binding source fires binding related events, so rather than having to manage binding related errors or checks on each control you can do it all in one place. But from an ease of use perspective this mechanism seems very klunky way to work.
I also think it's kind of a shame after all the discussion that went on a while back on this that the binder cannot bind to fields - it can only bind properties. I have many apps where there are pure data objects and Web Service proxy objects, which do not expose properties but only public fields and you can't bind against them. This seems a needless limitation...