DataBinding Problem

M

Martin

Hello,

i have a custom collection implementing IList. this collection holds custom
objects. my binding object is the collection. i implemented the
PropertyChanged events in my custom object.

example:
txtMyControl.DataBindings.Add("Text", myCustomCollection, "MyPropertyName");

the problem is, that the text editor isn't refreshed when the property
changed. the following works:

txtMyControl.DataBindings.Add("Text", myCustomCollection[sKey],
"MyPropertyName");

do anybody know why the second works or why the first don't work?

thanx!
 
B

Bart Mermuys

Hi,

Martin said:
Hello,

i have a custom collection implementing IList. this collection holds
custom objects. my binding object is the collection. i implemented the
PropertyChanged events in my custom object.

example:
txtMyControl.DataBindings.Add("Text", myCustomCollection,
"MyPropertyName");

the problem is, that the text editor isn't refreshed when the property
changed. the following works:

txtMyControl.DataBindings.Add("Text", myCustomCollection[sKey],
"MyPropertyName");

Custom object's notify the UI differently then custom lists.

In NET1.1, custom object's fire a "PropName+Changed" event and in NET2.0
custom object's can fire INotifyPropertyChanged.PropertyChanged.

In NET1.1, custom lists must implement IBindingList and fire
IBindingList.ListChanged when one of its items have changed. This means you
need to implement this yourself, eg. make it so that the custom objects know
which custom list they belong to, then when one of the object's properties
changes, the object calls a method on the custom list which will fire
IBindingList.ListChanged(ItemChanged).

The same is true for NET2.0, BUT you can use BindingList<T> as a custom list
or as a basis for a custom list. A BindingList<T> will catch the
INotifyPropertyChanged.PropertyChanged fired by any of it's objects and then
fire IBindingList.ListChanged (resulting in a correct update of UI).

Example (NET2.0)

public class CustomObject : INotifyPropertyChanged
{
private string name;
public PropertyChangedHandler PropertyChanged;

public string Name
{
get { return name; }
set
{
if ( name != value )
{
name = value;
if ( PropertyChanged!=null)
PropertyChanged(this, new PropertyChangedEventArgs("Name");
}
}
}
}

public class CustomList : BindingList<CustomObject>
{
// The BindingList implements IBindingList and will fire
// IBindingList.ListChanged when one of the items
// fired INotifyPropertyChanged.PropertyChanged.
// It also includes typical typed collection methods.
}

CustomList cl = new CustomList();

cl.Add ....
cl.Add....

txtMyControl.DataBindings.Add("Text", cl, "Name");


HTH,
Greetings
 

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