Say I have a composite control in which I have a ListBox control which lists, for example, employees, and another control which contains multiple TextBox controls that will contains the employee’s details.
When an employee is selected from the ListBox control, I need to pass an Employee object to the “detail” control, then assign the Text property of every TextBox control to their relative property of the Employee object.
I have two solutions to pass the Employee object to the “detail” control but I am not sure of which is the best.
Solution 1 : Expose a Employee property in the “detail” control so when an employee is selected, I could do detailControl.Employee = selectedEmployee.
Solution 2 : Expose an event in the main control and trigger it when an employee is selected. The “detail” control would subscribe to that event and receive the Employee object through the event’s eventargs.
I know that both method will work just fine. I am just unsure of what is best to use.
Some say that you should pass data between controls with custom events, that it is a best loosely-coupled approach.
Some say that implementing custom events takes more time because you have to create them, add the properties to it, etc. so going with exposed properties is faster and simpler.
What do you suggest ?
Well, in my ideal world you would have a controller of some kind.
The main control would raise an event to which the controller would listen. The controller would then decode who should be notified, i.e the detail control.
This decouples the UI components entirely, providing the usual benefits, including facilitated unit testing.