liveBook · Manning

Change handler

As explained in .NET MAUI in Action

A change handler is a callback method invoked when a property value changes so that dependent state can be updated.

A change handler is a callback invoked when a bindable property changes. It receives the control, old value, and new value, then updates dependent state. section 11.2.2

Enabling a custom stepper

A custom MildredStepper exposes an IsEnabled bindable property. Its handler casts the new value to bool and applies it to the value Entry, plus button, and minus button. A DataTrigger sets IsEnabled to false when SelectedProduct is null; selecting a product enables the stepper again. section 11.2.2

Implement the pattern in three steps:

  • Choose the default value needed after a trigger reverts; here, IsEnabled defaults to true. section 11.2.2
  • Define a handler with bindable, oldValue, and newValue parameters. section 11.2.2
  • Pass the handler by name using the propertyChanged argument of BindableProperty.Create. section 11.2.2
Listing 11.8 The OnIsEnabledChanged method
private static void OnIsEnabledChanged(BindableObject
 bindable, object oldValue, object newValue)              #1
 
{
    if (bindable is MildredStepper mildredStepper)          #2
    {
        mildredStepper.IsEnabled = (bool)newValue;          #3
 
        mildredStepper.ValueEntry.IsEnabled =
 mildredStepper.IsEnabled;                                #4
        mildredStepper.PlusButton.IsEnabled =
 mildredStepper.IsEnabled;                                #4
        mildredStepper.MinusButton.IsEnabled =
 mildredStepper.IsEnabled;                                #4
    }
}

Use the handler structure as a template for propagating a bindable property value to internal controls. section 11.2.2

How .NET MAUI in Action covers it

How deep it goes
Defines callback parameters and wiring, then demonstrates propagation, without covering broader lifecycle behavior.
Examples
Uses the IsEnabled property on a MildredStepper to synchronize an Entry and two buttons.
What it assumes
Assumes bindable-property and data-trigger basics while explaining default values and callback registration procedurally.
What you can do after
Build custom-control behavior that reacts to bindable property changes and keeps dependent controls synchronized.
How it connects
Connects bindable properties, data triggers, default values, custom controls, and data binding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET MAUI in Action. The section links point to the parts of the book it draws on.