Repository navigation
Open
Labels
area-xamlXAML, CSS, Triggers, Behaviorsgood first issueGood for newcomersproposal/opent/enhancement βοΈNew feature or request
Milestone
Description
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();
}
}
}Reacted by ben14044
Metadata
Metadata
Assignees
Labels
area-xamlXAML, CSS, Triggers, Behaviorsgood first issueGood for newcomersproposal/opent/enhancement βοΈNew feature or request
Activity
Egbaiyelo commentedon Mar 12, 2026
Hey, can I try this one
minahilatiqdev commentedon Sep 26, 2026
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.
jfversluis commentedon Sep 26, 2026
@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.
minahilatiqdev commentedon Sep 26, 2026
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.
pictos commentedon Sep 27, 2026
@minahilatiqdev , if you do what you need before calling the
basedoes it work? Because the BindingContext is only applied when calling thebasemethod, so doing anything before would behave asChanging, no?minahilatiqdev commentedon Sep 27, 2026
@pictos, good question. I checked the actual
BindableObjectsource to confirm rather than guess.The order of operations lives in
BindingContextPropertyChanged, which is the property-changed handler that fires wheneverBindingContextis set:ApplyBindings()runs beforeOnBindingContextChanged()is ever called.ApplyBindings()is what re-applies all the other bound properties (likeChartDataandRefin the example) to the new context, which in turn fires their ownPropertyChangedcallbacks immediately.So by the time
OnBindingContextChanged()runs at all, regardless of whether we put code before or after thebasecall inside it,ApplyBindings()has already executed, and dependent properties likeChartDatahave already picked up the new context's values and fired their change callbacks using the old_drawstate.Putting
_draw = falsebeforebase.OnBindingContextChanged()doesn't help, because the thing we need to guard against (the dependent property updates) has already happened one level up, beforeOnBindingContextChangedis invoked at all.A true
OnBindingContextChangingwould need to hook in beforeApplyBindings()runs, basically before theBindingContextPropertyChangedcallback does anything, which isn't something the currentOnBindingContextChangedoverride can reach into, no matter where we place code relative tobase.So I think the fix still needs a real hook at that point, something like:
That gives consumers a chance to react before dependent properties get re-applied, which is exactly the gap in the
SparklineChartexample above. Happy to put together a draft PR along these lines if that direction sounds right to you and @jfversluis.