Search code examples
c#blazormudblazor

How to call CheckedChanged from MudCheckBox control


Please help me.

I do not find any CheckedChanged example for MudCheckBox. I got the results related to input control only.

<input type="checkbox" @onchange="HandleCheck" />

I tried the documents but didn't get exact example for MudCheckBox. I'm able to bind successfully to the IsCheckedTrue property. This checkbox is inside the MudTable. All I want to call CheckedChanged event.

<MudCheckBox @bind-Checked="@context.IsCheckedTrue" Color="Color.Secondary" CheckedIcon="@Icons.Material.Filled.RadioButtonChecked" UncheckedIcon="@Icons.Material.Filled.RadioButtonUnchecked"></MudCheckBox>

Solution

  • Note that in MudBlazor Version 7 there are a considerable number of breaking changes. One is the parameter name is now Value not Checked. So the bind is to bind-Value. See https://github.com/MudBlazor/MudBlazor/issues/8447

    This should work using the new setters and getters.

    <MudCheckBox @bind-Checked:get=context.IsCheckedTrue @bind-Checked:set=HandleCheck />
    
    @code {
        private Task HandleCheck(bool value)
        {
            // Set the value in the model
            // Do what you want
            return Task.CompletedTask;
        }
    }
    

    Or setting the parameters directly:

    <MudCheckBox Checked=context.IsCheckedTrue CheckedChanged=HandleCheck />
    
    @code {
        private Task HandleCheck(bool value)
        {
            // Set the value in the model
            // Do what you want
            return Task.CompletedTask;
        }
    }
    

    You should also be able to do this, but there's been compiler problems with this syntax, so it may throw errors:

    <MudCheckBox @bind-Checked=context.IsCheckedTrue @bind-Value:after="HandleAfter" />
    
    @code {
        private Task HandleAfter()
        {
            // Do what you want
            return Task.CompletedTask;
        }
    }