any tips for trouble shooting databinding?

J

Jeremy Chaney

I'm writing an application in C# and I've used the IDE to bind a
checkbox to a boolean in my app. The properties for my checkbox in the
Visual Designer indicate that my databinding for the "Checked" state is
correct, but when I run my app, my button does not update to reflect the
state of the property I've bound it to. I've stepped through the code
and know that my data value is changing, but the UI does not reflect the
change.

Any ideas how to trouble shoot this? I've spent the last hour searching
Google and Google Groups, but I haven't found anything.

Thanks,
--Jeremy
 
P

PhilipDaniels

I'm writing an application in C# and I've used the IDE to bind a
checkbox to a boolean in my app. The properties for my checkbox in the
Visual Designer indicate that my databinding for the "Checked" state is
correct, but when I run my app, my button does not update to reflect the
state of the property I've bound it to. I've stepped through the code
and know that my data value is changing, but the UI does not reflect the
change.

Any ideas how to trouble shoot this? I've spent the last hour searching
Google and Google Groups, but I haven't found anything.

Thanks,
--Jeremy

It's probably that the control is not aware that the boolean has
changed. Where is the boolean: in a DataSet, a BindingList<> or a
single instance of your own business object?
 
J

Jeremy Chaney

It's probably that the control is not aware that the boolean has
changed. Where is the boolean: in a DataSet, a BindingList<> or a
single instance of your own business object?

It is in a property in one of my classes:

private bool m_bMyBool;
public bool MyBool
{
get { return m_bMyBool; }
set { m_bMyBool = value; }
}

Thanks,
--Jeremy
 
P

PhilipDaniels

It is in a property in one of my classes:

private bool m_bMyBool;
public bool MyBool
{
get { return m_bMyBool; }
set { m_bMyBool = value; }
}

Thanks,
--Jeremy

I think my initial suspicion was correct, though it's difficult to be
absolutely sure without seeing your app. Basically, at the moment the
bound control is ignorant of any change in the object. To make it
aware you need to tell it something has changed, which you do by
implementing the INotifyPropertyChanged interface on your object. From
the Visual Studio help:

<quote>
"The INotifyPropertyChanged interface is used to notify clients,
typically binding clients, that a property value has changed.

For example, consider a Person object with a property called
FirstName. To provide generic property-change notification, the Person
type implements the INotifyPropertyChanged interface and raises a
PropertyChanged event when FirstName is changed. If your data source
implements the INotifyPropertyChanged and you are performing
asynchronous operations you should not make changes to the data source
on a background thread. Instead, you should read the data on a
background thread and merge the data into a list on the UI thread.

For change notification to occur in a binding between a bound client
and a data source, your bound type should either:

Implement the INotifyPropertyChanged interface (preferred).

Provide a change event for each property of the bound type.

Do not do both."
</quote>

There is example code too.


To be honest, I have never bothered with implementing this interface
because my objects always need to live within some sort of collection.
If you make this a specialization of the BindingList generic type:

public class ListOfMyBools : BindingList<MyBool> { }

Then you can get all the 2 way binding "for free" on any type of
object. BindingList broadcasts changes at item level rather than
item.property level (see page 650 of "Windows Forms 2.0 Programming"
by Chris Sells and Michael Weinhardt).

If you really want to understand data binding then I recommend the
book "Data Binding With Windows Forms 2.0" by Brian Moyes, published
by Addison Wesley.
 
J

Jeremy Chaney

I think my initial suspicion was correct, though it's difficult to be
absolutely sure without seeing your app. Basically, at the moment the
bound control is ignorant of any change in the object. To make it
aware you need to tell it something has changed, which you do by
implementing the INotifyPropertyChanged interface on your object. From
the Visual Studio help:

<quote>
"The INotifyPropertyChanged interface is used to notify clients,
typically binding clients, that a property value has changed.

For example, consider a Person object with a property called
FirstName. To provide generic property-change notification, the Person
type implements the INotifyPropertyChanged interface and raises a
PropertyChanged event when FirstName is changed. If your data source
implements the INotifyPropertyChanged and you are performing
asynchronous operations you should not make changes to the data source
on a background thread. Instead, you should read the data on a
background thread and merge the data into a list on the UI thread.

For change notification to occur in a binding between a bound client
and a data source, your bound type should either:

Implement the INotifyPropertyChanged interface (preferred).

Provide a change event for each property of the bound type.

Do not do both."
</quote>

There is example code too.


To be honest, I have never bothered with implementing this interface
because my objects always need to live within some sort of collection.
If you make this a specialization of the BindingList generic type:

public class ListOfMyBools : BindingList<MyBool> { }

Then you can get all the 2 way binding "for free" on any type of
object. BindingList broadcasts changes at item level rather than
item.property level (see page 650 of "Windows Forms 2.0 Programming"
by Chris Sells and Michael Weinhardt).

If you really want to understand data binding then I recommend the
book "Data Binding With Windows Forms 2.0" by Brian Moyes, published
by Addison Wesley.

This is great info. I'll give it a try, and I'll check out the book you
recommend. Thank you.
--Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top