How to modify ContentDialog closing behavior

BitSmithy 1,606 Reputation points


I have ContentDialog with primary button and close button.
I want that when I press primary button the dialog doesnt be closed.
It should be closed only if close button was clicked.

Below my code:

    private async void btnSave_Click(object sender, RoutedEventArgs e)

        ContentDialog saveDialog = new ContentDialog
            CloseButtonText = "Cancel",

            IsPrimaryButtonEnabled = true,
            PrimaryButtonText = "Save",

    ContentDialogResult result = await saveDialog.ShowAsync();

        if (result == ContentDialogResult.Primary)
            I want run some code here, but does not close the ContentDialog

        The ContentDialog should be closed

            // The user clicked the CLoseButton, pressed ESC, Gamepad B, or the system back button.
            // Do nothing.

Please write, how to code such task

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. AryaDing-MSFT 2,841 Reputation points


    Welcome to Microsoft Q&A!

    You could set a global variable named sign, then set args.Cancel in ContentDialog.Closing event through it. As follows:

    public bool sign;  
    private void Dialog_CloseButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)  
           sign = false;            
    private void Dialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)  
                sign = true;  
               // Perform your action  
    private void Dialog_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)  
               if (sign)  
                    args.Cancel = true;             

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful