Binding Code and Data to MCML

Data binding is the way in which you bind code and data to MCML. ModelItem objects are code-supported objects that represent the most common abstract data and callback concepts and are strictly non-visual. These objects should be used as the main interface to the UI controls that are defined in your MCML markup.

The ModelItem class implements the IPropertyObject interface, and provides the following features:

  • Change notifications.

  • Direct simple bindings, which allow you to associate a property on your ModelItem object with a property on another object, or vice versa. For the ModelItem object to detect property changes on a different object, the other object must implement the IPropertyObject interface.

    For convenience, you can use the following ModelItem methods for binding to other code objects:

    • BindFromSource, which creates a binding between a remote source object and a ModelItem object. 
    • BindToTarget, which creates a one-way binding from a ModelItem object to a target.
    • TwoWayBind, which creates a two-way binding between a ModelItem object and a target.
  • Lifetime management. ModelItem objects are typically connected to many other objects through event handlers and other methods. To prevent common leaks, each ModelItem must be owned by an IModelItemOwner object. That owner then takes responsibility for disposing the owned ModelItem objects.

    The ModelItem class implements the IModelItemOwner interface; one ModelItem can own others, thereby ensuring that ModelItem objects that are being referenced by another ModelItem object are automatically disposed and released. For example, a page ModelItem object could own all of the ModelItem objects on the page.

Windows Media Center supports binding data from any .NET object, as well as generalized data sources through a set of data interfaces. If the source exposes IPropertyObject notifications, MCML rules can automatically respond.

Note   Your code cannot access UI elements—UI elements own the data-binding relationship, rather than the code owning it.

You can derive custom model items from the ModelItem class or from the IModelItem interface:

  • Deriving from the ModelItem class is easier and quicker. This class requires a constructor to be instantiated on the application thread. Objects derived from ModelItem cannot be remoted.
  • Deriving from the IModelItem interface requires more work; you must implement the entire interface and implement your own change notifications. However, deriving from this interface gives you more control and better performance for your application.

The Microsoft.MediaCenter.UI namespace provides the ModelItem class and the following derived classes that you can use directly:

Type Description
Choice Maintains a list and the item within it that is selected. This class is useful when you want to implement a radio button group.
BooleanChoice Maintains a list of two options (True and False) and the item that is selected. This class is useful when you want to implement a check box.
Command

InvokeCommand (MCML)

NavigateCommand (MCML)

Provides callback functionality so you can connect the UI to the program state. For example, use one of these classes with a UI button to receive click notifications.
EditableText Represents an editable string. This class is useful when you want to implement an edit box.
ListDataSet

ArrayListDataSet

Maintains a list of objects for a list or a gallery in the UI. These classes raise events for add, remove, and modified notifications, which are compatible with Repeater view items.
RangedValue

ByteRangedValue

IntRangedValue

Maintains a range of values. For example, use these classes to implement a spinner control or a slider control.
Timer Creates a configurable timer that raises notifications on clock ticks (that is, specific intervals).
PropertySet Provides a loosely-typed dictionary that raises notifications. However, when possible, use a strongly-typed structure instead. This class works well where data structures are required, but custom code is not possible (such as web applications).

Sample Explorer

  • Model Items > Secure Editable Text
  • Code and Data > Model Item
  • Code and Data > Threading
  • Code and Data > Virtualization

See Also