Wiki

Clone wiki

Data Bind / Data Context, Property and Collection

Data Context

The data context is the main concept on the logic side to provide the data which should be visualized on presentation side.

To create a new data context, just create a class which inherits from the Slash.Unity.DataBind.Core.Data.Context class.

#!c#
public class DataContext : Slash.Unity.DataBind.Core.Data.Context
{
}

That's all, now you are ready to create the properties which contain the data to be visualized.

Data Properties

The data properties are just wrappers around the data values which should be visualized on the presentation side. This is required so the presentation side gets informed if a data value changes and can update itself accordingly.

#!c#
private readonly Slash.Unity.DataBind.Core.Data.Property<string> descriptionProperty = new Slash.Unity.DataBind.Core.Data.Property<string>();

public string Description
{
    get
    {
        return this.descriptionProperty.Value;
    }
    set
    {
        this.descriptionProperty.Value = value;
    }
}

Naming Conventions

To make sure the bindings will find the data properties without changing the interface of the data context class, there is a naming convention for the data property. The bindings expect the data property for a data value to be named with the name of the data (e.g. Speed) plus "Property".

The lookup via reflection is done in this order:

  • Field with a lower case first letter (e.g. speedProperty)
  • Field with an upper case first letter (e.g. SpeedProperty)
  • Property with an upper case first letter (e.g. SpeedProperty)

Since version 1.0.8 there is a DataBindSettings asset in your Resources folder where you can set additional naming conventions.

You might not want to write this boilerplate code all the time when you introduce a new data property. That's why there are code snippets which just expect you to write the data type and data name and creates the data property in the correct way. Snippets are available for:

#!c#

private readonly Slash.Unity.DataBind.Core.Data.Property<$Type$> $name$Property = new Slash.Unity.DataBind.Core.Data.Property<$Type$>();

public $Type$ $Name$
{
    get
    {
        return this.$name$Property.Value;
    }
    set
    {
        this.$name$Property.Value = value;
    }
}

Data Collection

A collection is a special kind of data property type which allows to store a collection of objects, e.g. to show them with a ´Grid Items Setter´.

Like the data property capsules a single data value, the data collection capsules a collection of values and provides events to indicate that:

  • A value was inserted
  • A value was removed
  • The collection was cleared

Data Collection Code Snippets

There are special code snippets available for the data collection:

Updated