Francesco Belladonna
Francesco Belladonna

Reputation: 11689

Winform: Binding a custom control property to a BindingList

I'm trying to create a binding from my custom control to objects that are in a BindingList.

While with textbox, I can easily write

textBox.DataBindings.Add("Text",myBindingList,"PropertyOfObjectOfBindingList")

With my custom property "Value", this thing doesn't work (the object doesn't get updated).

What should I implement with my custom control to make it works? I already implemented INotifyPropertyChanged, but it doesn't work.

I just want make this line works:

customControl.DataBindings.Add("CustomProperty",myBindingList,"PropertyOfObjectOfBindingList")

EDIT 1:

I read this around web: http://kbalertz.com/327413/control-using-Visual.aspx however is not working for me at the moment, maybe I'm doing something wrong

Upvotes: 2

Views: 3074

Answers (3)

Francesco Belladonna
Francesco Belladonna

Reputation: 11689

I solved the problem by myself:

While the article I linked is a good suggestion, there is a wrong part; you don't have to create an event in your custom class with PropertyChangedEventHandler, but just with EventHandler.

public event EventHandler CustomPropertyChanged;

Is enough to make everything works. Obviusly you have to call it when your property changes

EDIT 1:

I discovered a bad thing, while on textboxes, if the control lose focus the bindinglist get updated, on my custom controls this thing happens only when I change selected item in listbox.

I don't find a way to solve this at the moment.

Upvotes: 1

k.m
k.m

Reputation: 31454

Since you said your bound object doesn't get updated (I assume from Control -> Object changes), but it is bound correctly, maybe this will help:

customControl.DataBindings.Add("CustomProperty", list, "BoundObjectProperty", 
    false, DataSourceUpdateMode.OnPropertyChanged);

Upvotes: 1

TcKs
TcKs

Reputation: 26632

Maybe the Implementing complex data binding in custom controls article will help.

Upvotes: 0

Related Questions