Project 1 had multiple ways to control the same thing (i.e.,
View->List/Details & ComboBox),
(View->Status Bar & Tools->Folder Options)Many systems have multiple
views onto a single "model" (i.e., Windows Explorer view onto
file system - try demo)
How did people solve this with Project 1?
Nice approach is to consider separate Model & View. View has to know
about Model, but
not vice versa. How is it possible? With events.
- Model can fire an event whenever the model changes
- Views can register with model to receive events (i.e., register
event handlers)
- Be careful: If a view modifies the model, it will also receive
an event when that change
is made, so it must be careful about being able to handle events
that reflect changes
that have already been applied.
- Inefficient approach: Event just says model changes, and each
view has to repopulate entire view
- Efficent approach: Event says *how* model changes, and views only
make incremental changes
List ModelView example
Data Binding
Built-in support for coupling between model and view
"Simple" data binding - a single data element and property
"Complex" data binding - multiple data elements
Some controls have built-in support, like ListBox
See DataSource, DisplayMember, ValueMember
DataBindingExample |