Skip to content

Enable the inheritance of the parent's BindingContext for Behaviors, based on a Flag #26705

@egvijayanand

Description

@egvijayanand

Description

While trying the v10 release of the MAUI Community Toolkit, faced CommunityToolkit/Maui#2391

Excerpt from the release notes:

  • .NET MAUI Behaviors do not automatically inherit their parent's BindingContext (a breaking change from Xamarin.Forms)
  • Going forward, the .NET MAUI Community Toolkit will follow this implementation of Behaviors

I'm not fully aware of the reasoning behind this design change, maybe to improve the performance, but ideally, it should have been put under a Flag with the default value now set to false.

In this EventToCommandBehavior scenario, it won't work without its BindingContext set. Simply toggling a Flag would be preferable than relatively adjusting the (binding) Source for the Command bound to the behavior, as the latter often has its own implications as described below.

For now, I'm tried to set the RelativeSource binding to inherit the parent's BindingContext, but it ended up in exception with the below message.

Cannot apply relative binding to CommunityToolkit.Maui.Behaviors.EventToCommandBehavior because it is not a superclass of Element.

Then tried with Reference binding, but XAMLC sends warning messages.

<mct:EventToCommandBehavior
    Command="{Binding x:DataType=vw:MainPage, Source={x:Reference page}, Path=BindingContext.ShowMessageCommand}"
    EventArgsConverter="{StaticResource ArgsConverter}"
    EventName="RawMessageReceived" />

Without x:DataType - \MauiApp1\Views\MainPage.xaml(76,29): XamlC warning XC0045: Binding: Property "BindingContext" not found on "MauiApp1.ViewModels.MainViewModel".

With x:DataType - \MauiApp1\Views\MainPage.xaml(76,29): XamlC warning XC0045: Binding: Property "ShowMessageCommand" not found on "System.Object".

To avoid this, I propose introducing a Flag that, when toggled, will inherit the parent's BindingContext.

Public API Changes

A boolean flag, as a property in the Behavior type, to determine whether to inherit the parent's BindingContext based on its value, with the default set to false to reflect the design change.

Intended Use-Case

As explained in the Description.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions