Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Enhancement] MultiBinding #4565

@legistek

Description

@legistek

Summary

Add a MultiBinding binding type equivalent to WPF.

API Changes

// new
public class MultiBinding : BindingBase
{
     public Collection<BindingBase> Bindings {get; set;}
     public IMultiValueConverter Converter {get; set;}
}

// new
public interface IMultiValueConverter 
{
     object Convert(object[] values, Type targetType, object parameter);
     object[] ConvertBack(object value, Type[] targetTypes, object parameter);
}

Intended Use Case

Example XAML usage:

<Button>
     <Button.IsEnabled>
          <MultiBinding Converter="{StaticResource AllMustBeTrueMultiConverter}">
               <Binding Path="ActionIsAllowed"/>
               <Binding Path="SomeOtherRequirement"/>
          </MultiBinding>
     </Button.IsEnabled>
</Button>

Comment - It's challenging to build highly responsive UIs in XAML without MultiBinding. Currently there are two workarounds, which even together do not address all cases:

(1) Define a single property in a view model that resolves to a value based on other properties, and bind in XAML to the single property.

First, this makes view models more complex and more difficult to maintain. Suppose a property ActionIsEnabled is defined as:

public bool ActionIsEnabled => this.ActionIsAllowed && this.SomeOtherRequirement;

Each of the setters for the independent properties (ActionIsAllowed and SomeOtherRequirement) need to include property change notifications for ActionIsEnabled in addition to themselves. If the logic for ActionIsEnabled is changed, however, then those other property setters (and/or those of other properties) have to potentially be updated as well.

Moreover, this assumes the dependencies are all entirely within the same view model. If the independent properties are in different classes than the dependent property, however, there may be no practical way to send a property change notification for the dependent property when the independent properties in the other classes are changed. This workaround therefore doesn't work in all cases.

(2) Wrap a control in a new layer (Grid, etc) for each condition.

I've seen this done a lot in Angular using nested <div>s for each condition. Leaving aside the inefficiency, this does work for simple cases, the most common one being a cascading property like IsEnabled or IsVisible, where all the conditions must evaluate to true. Anything more complex, however, and this system doesn't work either - for example, if ANY rather than ALL conditions must be true.

MultiBinding IMHO provides the most robust and elegant means of specifying complex dependencies between control properties and data.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions