Table of Contents

Advanced Bindings

Bindings provide a powerful and simple approach to use MVVM in your applications.

Bindings

Bind - an extension method that provides for any views that implement the interface IBindingsOwner (more details: Bindable)

XToolkit Bindings supports three types of bindings: OneTime, OneWay, TwoWay

One-Way

  • This binding mode transfers values from ViewModel to View;
  • Whenever the property changes within the ViewModel, the corresponding View property is automatically adjusted;
  • This binding mode is useful when showing, for example, data that is arriving from a dynamic source - like from a sensor or from a network data feed.

One-way bindings used by default.

this.Bind(() => ViewModel.FirstName, () => FirstNameLabel.Text);

Two-Way

  • This binding mode transfers values in both directions;
  • Changes in both View and ViewModel properties are monitored - if either change, then the other will be updated;
  • This binding mode is useful when editing entries in an existing form;
  • Have some default definitions for simple platform controls, like Label, UISwitch, TextView, for other cases provides a mechanism for define custom bindings (look below).
this.Bind(() => ViewModel.Count, () => CountField.Text, BindingMode.TwoWay);

One-Time

  • This binding mode transfers values from ViewModel to View;
  • This transfer doesn’t actively monitor change messages/events from the ViewModel;
  • Instead, this binding mode tries to transfer data from ViewModel to View only when the binding source is set. After this, the binding doesn’t monitor changes and doesn’t perform any updates, unless the binding source itself is reset;
  • This mode is not very commonly used but can be useful for fields that are configurable but which don’t tend to change after they have initially been set.

Extended Bind

this.Bind(() => ViewModel.Count, count => CountField.Text = count, BindingMode.TwoWay);

Value Converters

A ValueConverter is a class that implements the IConverter<TOut, TIn> interface.

IConverter<string, Person> _personConverter;

this.Bind(() => ViewModel.Person, () => NameLabel.Text, _personConverter);

Also, you can use ConverterBase<TOut, TIn> abstract class for common cases.

ValueConverters can also be provided with a parameter - this can sometimes be useful to reuse a single value converter in different situations.

Commands

Add command for default controls (UIButton, Button, ...):

OpenPageButton.SetCommand(ViewModel.OpenPageCommand);

Add command for any EventHandler:

linkSpan.SetCommand(nameof(linkSpan.Clicked), ViewModel.ClickHereCommand);

Collections

More details: iOS & Android Collections

  • .
  • .
  • // TODO
  • .
  • .

Rich examples

Applying two-way binding for iOS native control:

_datePicker = new UIDatePicker(CGRect.Empty);
_formatter = new NSDateFormatter();


var binding = this.SetBinding(
        () => ViewModel.PaymentDate,
        () => _datePicker.Date,
        BindingMode.TwoWay)
    // converter from NSDate to string
    .ConvertTargetToSource(date => _formatter.ToString(date))
    // observe custom event
    .ObserveTargetEvent(nameof(UIDatePicker.ValueChanged)));