[Proposal] Add RelativeBindingSource
Support to Typed Bindings Extensions
#200
Labels
blocked
champion
A member of the .NET MAUI Toolkit core team has chosen to champion this feature
needs discussion
The team will aim to discuss this at the next monthly standup
proposal
A fully fleshed out proposal describing a new feature in syntactic and semantic detail
Feature name
Add
RelativeBindingSource
Support to Typed BindingsLink to discussion
None
Progress tracker
Summary
This Proposal improves support for
RelativeBindingSource
to our TypedBinding extensions.This is not a breaking change and improves our API surface to better match .NET MAUI's TypedBinding API for its
TypedBinding.Source
property.RelativeBindingSource
is the recommended way for .NET MAUI developers to creating a binding referencing themselves or an ancestor without needing to pass in an instance of that objectRelativeBindingSource.Self
new RelativeBindingSource(RelativeBindingSourceMode.FindAncestor, typeof(Page))
)new RelativeBindingSource(RelativeBindingSourceMode.FindAncestorBindingContext, typeof(MyViewModel))
Current Workaround
The current workaround to use
RelativeBindingSource
with our existing TypedBinding APIs is to first use.Assign()
to assign the control to a variable, then pass that variable in as thesource
parameter.Motivation
All of our TypedBinding APIs (example below) require the
source
parameter type to match theTBindingContext
used in by thegetter
andsetter
parameter:Expression<Func<TBindingContext, TSource>> getter
Action<TBindingContext, TSource>? setter = null
TBindingContext? source = default
// Cannot be of typeRelativeBindingSource
Detailed Design
The best way to support
RelativeBindingSource
, is to lower theType
of the API'ssource
parameter fromTBindingContext?
toobject?
.Using
object?
for thesource
parameter matches the .NET MAUI API for theTypedBinding.Source
property.Current Source Parameter
Updated Source Parameter
Example Updated API
Usage Syntax
Drawbacks
I don't see any drawbacks.
We will need to update our Unit Tests to validate
RelativeBindingSource
.We will also need to update our Unit Tests with invalid
source
parameters to ensure .NET MAUI throws an exception if a user provides an invalidsource
parameter to our API now that it supports anyobject
type.Alternatives
An alternative to lowering our existing API surface from
TBindingContext? source = default
toobject? source = null
would be to create new APIs specific toRelativeBindingSource
(example below).I do not recommend this approach for two reasons:
TBindingContext? source = default
and a second TypedBinding API forobject? source = null
object?
better matches .NET MAUI's TypedBinding.Source API (public object? Source
)Example API using
RelativeBindingSource
Unresolved Questions
None
The text was updated successfully, but these errors were encountered: