The following examples shows some BindingMode use cases.
C# Example
public class PersonViewModel
{
public string Name { get; set; }
public string Company { get; set; }
}
Label label;
PersonViewModel viewmodel;
//BindingMode.OneWay
label = new Label ();
label.BindingContext = viewmodel = new PersonViewModel ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name, mode: BindingMode.OneWay);
viewmodel.Name = "John Doe";
Debug.WriteLine (label.Text); //prints "John Doe"
label.Text = "Foo";
Debug.WriteLine (viewmodel.Name); //prints "John Doe"
//BindingMode.TwoWay
label = new Label ();
label.BindingContext = viewmodel = new PersonViewModel ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name, mode: BindingMode.TwoWay);
viewmodel.Name = "John Doe";
Debug.WriteLine (label.Text); //prints "John Doe"
label.Text = "Foo";
Debug.WriteLine (viewmodel.Name); //prints "Foo"
//BindingMode.OneWayToSource
label = new Label ();
label.BindingContext = viewmodel = new PersonViewModel ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name, mode: BindingMode.OneWayToSource);
viewmodel.Name = "John Doe";
Debug.WriteLine (label.Text); //prints ""
label.Text = "Foo";
Debug.WriteLine (viewmodel.Name); //prints "Foo"
| Member Name | Description |
|---|---|
| Default | When used in Bindings, indicates that the Binding should use the BindableProperty.DefaultBindingMode. When used in BindableProperty declaration, defaults to BindingMode.OneWay. |
| OneWay | Indicates that the binding should only propagates changes from source (usually the View Model) to target (the BindableObject). This is the default mode for most BindableProperty values. |
| OneWayToSource | Indicates that the binding should only propagates changes from target (the BindableObject) to source (usually the View Model). This is mainly used for read-only BindableProperty values. |
| TwoWay | Indicates that the binding should propagates changes from source (usually the View Model) to target (the BindableObject) in both directions. |