简体   繁体   中英

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

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM