Share via


BindableObjectExtensions.SetBinding Method

Definition

Overloads

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)
Obsolete.

Creates and applies a binding from an expression.

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 = default, string stringFormat = default);
static member SetBinding : Xamarin.Forms.BindableObject * Xamarin.Forms.BindableProperty * string * Xamarin.Forms.BindingMode * Xamarin.Forms.IValueConverter * string -> unit

Parameters

targetProperty
BindableProperty

The BindableProperty on which to set a binding.

path
System.String

A System.String indicating the property path to bind to.

mode
BindingMode

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

converter
IValueConverter

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

stringFormat
System.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.

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

// ...

var vm = new PersonViewModel {
    Name = "John Doe", 
    Company = "Xamarin"
}

var label = new Label ();
label.SetBinding (Label.TextProperty, "Name"); // "Name" is the property on the view model
label.BindingContext = vm;

Debug.WriteLine (label.Text); // prints "John Doe"

Applies to

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

Caution

This API is now deprecated.

Creates and applies a binding from an expression.

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[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 = default, string stringFormat = default);
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

The BindableObject.

targetProperty
BindableProperty

The BindableProperty to bind to

sourceProperty
System.Linq.Expressions.Expression<System.Func<TSource,System.Object>>

An expression used to retrieve the source path.

mode
BindingMode

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

converter
IValueConverter

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

stringFormat
System.String

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

Attributes
System.ComponentModel.EditorBrowsableAttribute System.ObsoleteAttribute

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 vm = new PersonViewModel {
    Name = "John Doe", 
    Company = "Xamarin"
};

var label = new Label ();
label.SetBinding<PersonViewModel> (Label.TextProperty, vm => vm.Name);  // "Name" is the property on the view model
label.BindingContext = vm;

Debug.WriteLine (label.Text); // prints "John Doe"

Applies to