winforms databinding best practices
Posted
by Kaiser Soze
on Stack Overflow
See other posts from Stack Overflow
or by Kaiser Soze
Published on 2010-03-16T19:35:32Z
Indexed on
2010/03/16
20:01 UTC
Read the original article
Hit count: 1216
Demands / problems:
- I would like to bind multiple properties of an entity to controls in a form. Some of which are read only from time to time (according to business logic).
- When using an entity that implements
INotifyPropertyChanged
as theDataSource
, every change notification refreshes all the controls bound to that data source (easy to verify - just bind two properties to two controls and invoke a change notification on one of them, you will see that both properties are hit and reevaluated). - There should be user friendly error notifications (the entity implements
IDataErrorInfo
). (probably usingErrorProvider
)
Using the entity as the DataSource
of the controls leads to performance issues and makes life harder when its time for a control to be read only.
I thought of creating some kind of wrapper that holds the entity and a specific property so that each control would be bound to a different DataSource
. Moreover, that wrapper could hold the ReadOnly
indicator for that property so the control would be bound directly to that value.
The wrapper could look like this:
interface IPropertyWrapper : INotifyPropertyChanged, IDataErrorInfo
{
object Value { get; set; }
bool IsReadOnly { get; }
}
But this means also a different ErrorProvider
for each property (property wrapper)
I feel like I'm trying to reinvent the wheel... What is the 'proper' way of handling complex binding demands like these?
Thanks ahead.
© Stack Overflow or respective owner