Skip to content

Add OnBindingContextChangingΒ #24804

Open
Open

Description

@michalpobuta

Description

I need place in my app to add code before Binding Context changed. As result i should be able to override method OnBindingContextChaging in ContentPage or ContentView.

Public API Changes

public partial class ExamplePage : ContentPage
{
public ExamplePage(ExampleViewModel vm)
{
    BindingContext = vm;
    InitializeComponent();
}
protected override void OnBindingContextChanging(BindingContextChangingEventArgs args)
    {
        //Action before binding context changed
        base.OnBindingContextChanging(args);
    }
}

Intended Use-Case

In my app, I have a sizable CollectionView, where each element is a custom control based on the View. While scrolling, the BindingContext for each element changes dynamically. However, I am only able to respond to the BindingContext after it has changed, and I am unable to react before the context is updated.

example:

public class SparklineChart : GraphicsView
{
    private bool _draw;

    public QuotesData[]? ChartData
    {
        get => (QuotesData[]?)GetValue(ChartDataProperty);
        set => SetValue(ChartDataProperty, value);
    }

    public decimal? Ref
    {
        get => (decimal?)GetValue(RefProperty);
        set => SetValue(RefProperty, value);
    }

    public static readonly BindableProperty ChartDataProperty = BindableProperty.Create(
        propertyName: nameof(ChartData),
        returnType: typeof(QuotesData[]),
        declaringType: typeof(SparklineChart),
        propertyChanged: ChartDataPropertyChanged,
        defaultBindingMode: BindingMode.OneWay);

    public static readonly BindableProperty RefProperty = BindableProperty.Create(
        propertyName: nameof(Ref),
        returnType: typeof(decimal?),
        declaringType: typeof(SparklineChart),
        propertyChanged: RefPropertyChanged,
        defaultBindingMode: BindingMode.OneWay);

    protected override void OnBindingContextChanging(BindingContextChangingEventArgs args)
    {
        _draw = false;
       base.OnBindingContextChanging(args);
    }

    protected override void OnBindingContextChanged()
    {
        _draw = true;
        sparklineChart.Invalidate();
        base.OnBindingContextChanged();
    }

    private static void ChartDataPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if (bindable is not SparklineChart { Drawable: Sparkline sparkline } sparklineChart)
        {
            return;
        }
        sparkline.ChartData = newValue as QuotesData[];

        if (_draw)
        {
            sparklineChart.Invalidate();
        }
    }

    private static void RefPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        if (bindable is not SparklineChart { Drawable: Sparkline sparkline } sparklineChart)
        {
            return;
        }
        sparkline.Ref = newValue as decimal?;

        if (_draw)
        {
            sparklineChart.Invalidate();
        }
    }
}

Activity

  1. added this to the Backlog milestone on Sep 30, 2024
  2. added
    area-xaml
    good first issue
    on Dec 9, 2024
  3. Egbaiyelo commented on Mar 12, 2026

    @Egbaiyelo

    Hey, can I try this one

  4. minahilatiqdev commented on Sep 26, 2026

    @minahilatiqdev

    Hi @michalpobuta @jfversluis,

    Is this issue still available? I noticed @Egbaiyelo expressed interest back in March but I don't see a linked PR yet. I'd love to work on this if it's still open β€” this would be my first contribution to MAUI.

  5. jfversluis commented on Sep 26, 2026

    @jfversluis
    Member

    @minahilatiqdev thanks for your interest! I don't think this ever happened. It's not a great time to start this now though. We can't add it to .NET 11 anymore and we don't really have a branch for .NET 12 yet :) so happy to take your contribution, but it might take a little while before you can get started. I'll add it to the .NET 12 planning milestone so we can talk about it with the team if this is something we would want.

  6. minahilatiqdev commented on Sep 26, 2026

    @minahilatiqdev

    Thanks for the update, @jfversluis! Totally understand the timing constraints. Please tag me once the .NET 12 branch is set up and the team decides to move forward.
    In the meantime, if you know of any other good first issues you'd recommend, I'd be happy to take a look. Still fairly new to contributing here, so open to whatever would be a good starting point.

  7. pictos commented on Sep 27, 2026

    @pictos
    Contributor

    @minahilatiqdev , if you do what you need before calling the base does it work? Because the BindingContext is only applied when calling the base method, so doing anything before would behave as Changing, no?

  8. minahilatiqdev commented on Sep 27, 2026

    @minahilatiqdev

    @pictos, good question. I checked the actual BindableObject source to confirm rather than guess.

    The order of operations lives in BindingContextPropertyChanged, which is the property-changed handler that fires whenever BindingContext is set:

    static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        bindable._inheritedContext = null;
        bindable.ApplyBindings(fromBindingContextChanged: true);
        bindable.OnBindingContextChanged();
    }

    ApplyBindings() runs before OnBindingContextChanged() is ever called. ApplyBindings() is what re-applies all the other bound properties (like ChartData and Ref in the example) to the new context, which in turn fires their own PropertyChanged callbacks immediately.

    So by the time OnBindingContextChanged() runs at all, regardless of whether we put code before or after the base call inside it, ApplyBindings() has already executed, and dependent properties like ChartData have already picked up the new context's values and fired their change callbacks using the old _draw state.

    Putting _draw = false before base.OnBindingContextChanged() doesn't help, because the thing we need to guard against (the dependent property updates) has already happened one level up, before OnBindingContextChanged is invoked at all.

    A true OnBindingContextChanging would need to hook in before ApplyBindings() runs, basically before the BindingContextPropertyChanged callback does anything, which isn't something the current OnBindingContextChanged override can reach into, no matter where we place code relative to base.

    So I think the fix still needs a real hook at that point, something like:

    static void BindingContextPropertyChanged(BindableObject bindable, object oldvalue, object newvalue)
    {
        bindable._inheritedContext = null;
        bindable.OnBindingContextChanging(new BindingContextChangingEventArgs(oldvalue, newvalue));
        bindable.ApplyBindings(fromBindingContextChanged: true);
        bindable.OnBindingContextChanged();
    }

    That gives consumers a chance to react before dependent properties get re-applied, which is exactly the gap in the SparklineChart example above. Happy to put together a draft PR along these lines if that direction sounds right to you and @jfversluis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamlgood first issueproposal/opent/enhancement β˜€οΈ

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions