BindableObjectExtensions.SetBinding BindableObjectExtensions.SetBinding Method

Definition

Overloads

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String) SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

Creates and applies a binding to a property.

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String) SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)

Creates and applies a binding from an expression.

SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String) SetBinding(BindableObject, BindableProperty, String, BindingMode, IValueConverter, String)

Creates and applies a binding to a property.

public static void SetBinding (this Xamarin.Forms.BindableObject self, Xamarin.Forms.BindableProperty targetProperty, string path, Xamarin.Forms.BindingMode mode = Xamarin.Forms.BindingMode.Default, Xamarin.Forms.IValueConverter converter = null, string stringFormat = null);
static member SetBinding : Xamarin.Forms.BindableObject * Xamarin.Forms.BindableProperty * string * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * string -> unit
Parameters
targetProperty
BindableProperty BindableProperty

The BindableProperty on which to set a bindinge.

path
String String

A String indicating the property path to bind to.

mode
BindingMode BindingMode

The BindingMode for the binding. This parameter is optional. Default is Default.

converter
IValueConverter IValueConverter

An IValueConverter for the binding. This parameter is optional. Default is null.

stringFormat
String String

A string used as stringFormat for the binding. This parameter is optional. Default is null.

Remarks

The following example shows how to use the extension method to set a binding.


var label = new Label ();
label.SetBinding (Label.TextProperty, "Name");
label.BindingContext = new {Name = "John Doe", Company = "Xamarin"};
Debug.WriteLine (label.Text); //prints "John Doe"

SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String) SetBinding<TSource>(BindableObject, BindableProperty, Expression<Func<TSource,Object>>, BindingMode, IValueConverter, String)

Warning

This API is now obsolete.

Creates and applies a binding from an expression.

[System.Obsolete]
public static void SetBinding<TSource> (this Xamarin.Forms.BindableObject self, Xamarin.Forms.BindableProperty targetProperty, System.Linq.Expressions.Expression<Func<TSource,object>> sourceProperty, Xamarin.Forms.BindingMode mode = Xamarin.Forms.BindingMode.Default, Xamarin.Forms.IValueConverter converter = null, string stringFormat = null);
static member SetBinding : Xamarin.Forms.BindableObject * Xamarin.Forms.BindableProperty * System.Linq.Expressions.Expression<Func<'Source, obj>> * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * string -> unit
Type Parameters
TSource

The source type.

Parameters
self
BindableObject BindableObject

The BindableObject.

targetProperty
BindableProperty BindableProperty

The BindableProperty to bind to

sourceProperty
Expression<Func<TSource,Object>>

An expression used to retrieve the source path.

mode
BindingMode BindingMode

The BindingMode for the binding. This parameter is optional. Default is Default.

converter
IValueConverter IValueConverter

An IValueConverter for the binding. This parameter is optional. Default is null.

stringFormat
String String

A string used as stringFormat for the binding. This parameter is optional. Default is null.

Remarks

This extension method uses Expression instead of path to creates and sets bindings. Using Expressions is more refactoring friendly.

This following example illustrates the setting of a binding using the extension method.


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

var label = new Label ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name);
label.BindingContext = new PersonViewModel {
  Name = "John Doe", 
  Company = "Xamarin"
};
Debug.WriteLine (label.Text); //prints "John Doe"

Applies to