CommunityToolkit/Maui.Markup

[Proposal] Further enhance `Bind` typed binding support to provide `IValueConverter` support

bijington opened this issue · 1 comments

Feature name

Further enhance Bind typed binding support to provide IValueConverter support

Link to discussion

I haven't created one

Progress tracker

  • Android Implementation
  • iOS Implementation
  • MacCatalyst Implementation
  • Windows Implementation
  • Tizen Implementation
  • Unit Tests
  • Samples
  • Documentation: MicrosoftDocs/CommunityToolkit#226

Summary

Currently the Bind extension method provides an override to pass in an IValueConverter implementation and this works great with all of the toolkits converters. The new typed bindings implementation does not provide this support. I propose that we fix that.

Motivation

Faster bindings!

Detailed Design

/// <summary>Bind to a specified property with inline conversion and conversion parameter</summary>
public static TBindable Bind<TBindable, TBindingContext, TSource, TParam, TDest>(
    this TBindable bindable,
    BindableProperty targetProperty,
    Func<TBindingContext, TSource> getter,
    (Func<TBindingContext, object?>, string)[] handlers,
    Action<TBindingContext, TSource>? setter = null,
    BindingMode mode = BindingMode.Default,
    IValueConverter converter = null,
    TParam? converterParameter = default,
    string? stringFormat = null,
    TBindingContext? source = default,
    TDest? targetNullValue = default,
    TDest? fallbackValue = default) where TBindable : BindableObject
{
    bindable.SetBinding(targetProperty, new TypedBinding<TBindingContext, TSource>(bindingContext => (getter(bindingContext), true), setter, handlers.Select(x => x.ToTuple()).ToArray())
    {
        Mode = mode,
        Converter = converter,
        ConverterParameter = converterParameter,
        StringFormat = stringFormat,
        Source = source,
        TargetNullValue = targetNullValue,
        FallbackValue = fallbackValue
    });

    return bindable;
}

Usage Syntax

new Label()
    .Bind(
    Label.TextProperty,
    static (ViewModel vm => vm.MyFavoriteColor,
    converter: new ColorToByteAlphaConverter())

Drawbacks

No response

Alternatives

No response

Unresolved Questions

No response

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.