Share via


Binding Constructors

Definition

Overloads

Binding()

Constructs and initializes a new instance of the Binding class.

Binding(String, BindingMode, IValueConverter, Object, String, Object)

Constructs and initializes a new instance of the Binding class.

Binding()

Constructs and initializes a new instance of the Binding class.

public Binding ();

Applies to

Binding(String, BindingMode, IValueConverter, Object, String, Object)

Constructs and initializes a new instance of the Binding class.

public Binding (string path, Xamarin.Forms.BindingMode mode = Xamarin.Forms.BindingMode.Default, Xamarin.Forms.IValueConverter converter = default, object converterParameter = default, string stringFormat = default, object source = default);
new Xamarin.Forms.Binding : string * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * obj * string * obj -> Xamarin.Forms.Binding

Parameters

path
System.String

The property path.

mode
BindingMode

The binding mode. This property is optional. Default is Default.

converter
IValueConverter

The converter. This parameter is optional. Default is null.

converterParameter
System.Object

An user-defined parameter to pass to the converter. This parameter is optional. Default is null.

stringFormat
System.String

A String format. This parameter is optional. Default is null.

source
System.Object

An object used as the source for this binding. This parameter is optional. Default is null.

Remarks

The following example shows how to set a binding to a property with a BindingMode and Converter:

public class PersonViewModel
{
  public string Name { get; set; }
  public string Company { get; set; }
}

public class ReverseConverter : IValueConverter
{
  public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    var s = value as string;
    if (s == null)
      return value;
    return new string (s.Reverse ().ToArray ());
  }

  public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    var s = value as string;
    if (s == null)
      return value;
    return new string (s.Reverse ().ToArray ());
  }
}

var label = new Label ();
PersonViewModel person;
label.BindingContext = person = new PersonViewModel { Name = "John Doe", Company= "Xamarin" };
label.SetBinding (Label.TextProperty, new Binding ("Name", mode: BindingMode.TwoWay, converter: new ReverseConverter ()));
Debug.WriteLine (label.Text); //prints "eoD nhoJ". ReverseConverter.Convert () is invoked in this case.

label.Text = "ooF";
Debug.WriteLine (person.Name); //prints "Foo". ReverseConverter.ConvertBack () is invoked in this case. The label Text change is propagated back as the BindingMode is TwoWay.

Applies to