MudBlazor: Prevent closing MudDialog when clicking on OK button

Viewed 1155

Is there a way to prevent closing of the MudDialog when the user click the OK button? I would like to keep the dialog opened while a action is performed.

private async Task OpenMyDialogAsync()
{
    var dialog = DialogService.Show<MyDialog>("My Dialog");
    var result = await dialog.Result;


    if (!result.Cancelled)
    {
        // Prevent closing the dialog when user click the OK button
        // to perform an action
    }
}

An other idea was to register on an event at the MyDialog instance. But the result returned by the Show() method don't contain the MyDialog instance. Any ideas how I can solve this?

1 Answers

Why close it then? Your "Ok" button has an OnClick.

Instead of this:

<MudButton Color="Color.Primary" OnClick="Submit">Ok</MudButton>
void Submit() => MudDialog.Close(DialogResult.Ok(true));

Do this:

<MudButton Color="Color.Primary" OnClick="RunMyStuff">Ok</MudButton>
    
void RunMyStuff(){
    // Run your stuff here
    // Done
    Submit();
}

void Submit() => MudDialog.Close(DialogResult.Ok(true));
Related