BindableObjectExtensions.SetBinding Method

Definition

Creates and applies a binding to a property.

public static void SetBinding (this Microsoft.Maui.Controls.BindableObject self, Microsoft.Maui.Controls.BindableProperty targetProperty, string path, Microsoft.Maui.Controls.BindingMode mode = Microsoft.Maui.Controls.BindingMode.Default, Microsoft.Maui.Controls.IValueConverter converter = default, string stringFormat = default);
static member SetBinding : Microsoft.Maui.Controls.BindableObject * Microsoft.Maui.Controls.BindableProperty * string * Microsoft.Maui.Controls.BindingMode * Microsoft.Maui.Controls.IValueConverter * string -> unit
<Extension()>
Public Sub SetBinding (self As BindableObject, targetProperty As BindableProperty, path As String, Optional mode As BindingMode = Microsoft.Maui.Controls.BindingMode.Default, Optional converter As IValueConverter = Nothing, Optional stringFormat As String = Nothing)

Parameters

targetProperty
BindableProperty

The BindableProperty on which to set a binding.

path
String

A 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
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