CMSC 498B: Developing User Interfaces - Spring 2005

Model-View and Data Binding

Focus management

  • Single global destination for keyboard input
  • Fundamentally different than mouse input which goes to Control under mouse pointer
  • Windows & Mac model is that keyboard focus tied to *active* window (on top)
  • X11 separates focus and top-most window and offers pluggable "Window Manager".
    Windows & Mac own the window manager
  • It is possible to plug in multiple mice and keyboard - what should happen?
    On Windows, each device type is combined (all mice drive single pointer, etc.)
    It is possible, using "raw" input API to get individual device event streams

.NET API

  • Control.CanFocus property (Visible, Enabled, and mapped)
  • ContainsFocus property (true if self or descendant has focus)
  • Focused property (true if self has focus)
  • Focus() Sets input focus to this control
    Returns false if fails (self or ancestor not mapped, visible, enabled, ControlStyles.Selectable true.
    Also these controls not selectable: Panel, GroupBox, PictureBox, ProgressBar, Splitter, Label, LinkLabel)
  • GotFocus event fires when control receives focus
  • LostFocus event fires when control loses focus
     

Models & Views

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

thin form sub-class: