How to enable data binding for a domain

All elements of a domain are 'binding' ready, but it's not enabled by default.

Enabling the binding mechanism means raising PropertyChanged event when a property value has changed and a CollectionChanged event when a relationship has changed.

Note : The INotifyDataErrorInfo is also implemented.

There are two ways to activate binding depending on how you define your domain.

Enabling binding by code

If you have defined your domain by code, you must declare for each element INotifyPropertyChanged and use ObservableModelElementCollection for each reference.

That's all. Due to the underlying event notifications implemented by Hyperstore, if an element has INotifyPropertyChanged in its declaration, all the notifications will be raised.

Exemple :

Binding is not activate for this class.

public partial class Library : ModelElement
{
    private ICollection<Book> _books;
    
    ctor...

    protected override void Initialize(ISchemaElement metadata, IDomainModel domainModel)
    {
        base.Initialize(metadata, domainModel);
        _books = new ModelElementCollection<Book>(this, "LibraryHasBooks", false);
    }

    public string Name { 
        get { return GetPropertyValue<string>("Name"); }
        set { SetPropertyValue("Name", value); }
    }

    public ICollection<Book> Books
    {
        get { return _Books; }
    }
}

Activate binding with adding INotifyPropertyChanged and using ObservableModelElementCollection

public partial class Library : ModelElement, INotifyPropertyChanged
{
    private ICollection<Book> _books;
    
    ctor...

    protected override void Initialize(ISchemaElement metadata, IDomainModel domainModel)
    {
        base.Initialize(metadata, domainModel);
        _books = new ObservableModelElementCollection<Book>(this,"LibraryHasBooks",false);
    }

    public string Name { 
        get { return GetPropertyValue<string>("Name"); }
        set { SetPropertyValue("Name", value); }
    }

    public ICollection<Book> Books
    {
        get { return _Books; }
    }
}

Enabling binding with the textual domain definition language

Nothing can be more easy, just add the observable attribute to your domain, like this :

[observable]
domain Hyperstore.Samples.MyDomain
{
 ....
}

In fact, with this attribute, all model element classes will be generated with the INotifyPropertyChanged declaration.


Last edited Apr 17, 2014 at 9:51 AM by aguerot, version 10