BindableObjectExtensions.SetBinding Method

Definition

Overloads

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

Creates and applies a binding to a property.

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

Creates a binding between a property on the source object and a property on the target object.

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

Source:
BindableObjectExtensions.cs
Source:
BindableObjectExtensions.cs

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

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

Source:
BindableObjectExtensions.cs

Creates a binding between a property on the source object and a property on the target object.

public static void SetBinding<TSource,TProperty> (this Microsoft.Maui.Controls.BindableObject self, Microsoft.Maui.Controls.BindableProperty targetProperty, Func<TSource,TProperty> getter, Microsoft.Maui.Controls.BindingMode mode = Microsoft.Maui.Controls.BindingMode.Default, Microsoft.Maui.Controls.IValueConverter? converter = default, object? converterParameter = default, string? stringFormat = default, object? source = default, object? fallbackValue = default, object? targetNullValue = default);
static member SetBinding : Microsoft.Maui.Controls.BindableObject * Microsoft.Maui.Controls.BindableProperty * Func<'Source, 'Property> * Microsoft.Maui.Controls.BindingMode * Microsoft.Maui.Controls.IValueConverter * obj * string * obj * obj * obj -> unit
<Extension()>
Public Sub SetBinding(Of TSource, TProperty) (self As BindableObject, targetProperty As BindableProperty, getter As Func(Of TSource, TProperty), Optional mode As BindingMode = Microsoft.Maui.Controls.BindingMode.Default, Optional converter As IValueConverter = Nothing, Optional converterParameter As Object = Nothing, Optional stringFormat As String = Nothing, Optional source As Object = Nothing, Optional fallbackValue As Object = Nothing, Optional targetNullValue As Object = Nothing)

Type Parameters

TSource

The source type.

TProperty

The property type.

Parameters

targetProperty
BindableProperty

The BindableProperty on which to set a binding.

getter
Func<TSource,TProperty>

An getter method used to retrieve the source property.

mode
BindingMode

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

converter
IValueConverter

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

converterParameter
Object

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

stringFormat
String

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

source
Object

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

fallbackValue
Object

The value to use instead of the default value for the property, if no specified value exists.

targetNullValue
Object

The value to supply for a bound property when the target of the binding is null.

Exceptions

Remarks

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

public class PersonViewModel
{
    public string Name { get; set; }
    public Address? Address { get; set; }
    // ...
}

var vm = new PersonViewModel { Name = "John Doe" };

var label = new Label();
label.SetBinding(Label.TextProperty, static (PersonViewModel vm) => vm.Name);
label.BindingContext = vm;

vm.Name = "Jane Doe";
Debug.WriteLine(label.Text); // prints "Jane Doe"

Not all methods can be used to define a binding. The expression must be a simple property access expression. The following are examples of valid and invalid expressions:

// Valid: Property access
static (PersonViewModel vm) => vm.Name;
static (PersonViewModel vm) => vm.Address?.Street;

// Valid: Array and indexer access
static (PersonViewModel vm) => vm.PhoneNumbers[0];
static (PersonViewModel vm) => vm.Config["Font"];

// Valid: Casts
static (Label label) => (label.BindingContext as PersonViewModel).Name;
static (Label label) => ((PersonViewModel)label.BindingContext).Name;

// Invalid: Method calls
static (PersonViewModel vm) => vm.GetAddress();
static (PersonViewModel vm) => vm.Address?.ToString();

// Invalid: Complex expressions
static (PersonViewModel vm) => vm.Address?.Street + " " + vm.Address?.City;
static (PersonViewModel vm) => $"Name: {vm.Name}";

Applies to