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.
Summary
Add a
MultiBindingbinding type equivalent to WPF.API Changes
Intended Use Case
Example XAML usage:
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
ActionIsEnabledis defined as:Each of the setters for the independent properties (
ActionIsAllowedandSomeOtherRequirement) need to include property change notifications forActionIsEnabledin addition to themselves. If the logic forActionIsEnabledis 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 likeIsEnabledorIsVisible, where all the conditions must evaluate totrue. Anything more complex, however, and this system doesn't work either - for example, if ANY rather than ALL conditions must be true.MultiBindingIMHO provides the most robust and elegant means of specifying complex dependencies between control properties and data.