Home > Net >  How can i check if the CloseButton is clicked in a ContentDialog in WinUI3
How can i check if the CloseButton is clicked in a ContentDialog in WinUI3

Time:12-25

I want to check if the CloseButton is clicked in a ContentDialog.

var inputTextBox = new TextBox
{
    AcceptsReturn = true,
    Height = 32,
    Width = 300,
    Text = string.Empty,
    TextWrapping = TextWrapping.Wrap,
};

if (Controller.CheckVersion() == 1)
{
    ContentDialog dialog = new ContentDialog
    {
        XamlRoot = this.XamlRoot,
        Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
        Title = "Please write something!",
        Content = inputTextBox,
        CloseButtonText = "Select",
    };

#pragma warning restore IDE0090

    _ = await dialog.ShowAsync();
}

Is there any Methods like IsClicked() for the ContentDialog in WinUI3? So I can check if the user has wrote something in the TextBox.

CodePudding user response:

A pretty simple example is included in MS documentation:

private async void ShowTermsOfUseContentDialogButton_Click(object sender, RoutedEventArgs e)
{
    ContentDialogResult result = await termsOfUseContentDialog.ShowAsync();
    if (result == ContentDialogResult.Primary)
    {
        // Terms of use were accepted.
    }
    else
    {
        // User pressed Cancel, ESC, or the back arrow.
        // Terms of use were not accepted.
    }
}

private void TermsOfUseContentDialog_Opened(ContentDialog sender, ContentDialogOpenedEventArgs args)
{
    // Ensure that the check box is unchecked each time the dialog opens.
    ConfirmAgeCheckBox.IsChecked = false;
}

private void ConfirmAgeCheckBox_Checked(object sender, RoutedEventArgs e)
{
    termsOfUseContentDialog.IsPrimaryButtonEnabled = true;
}

private void ConfirmAgeCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    termsOfUseContentDialog.IsPrimaryButtonEnabled = false;
}

ContentDialog Class

CodePudding user response:

You can use the PrimaryButton for Select and use the CloseButton for Cancel. You also can enable/disable the Select button according to the text input.

var inputTextBox = new TextBox
{
    AcceptsReturn = true,
    Height = 32,
    Width = 300,
    Text = string.Empty,
    TextWrapping = TextWrapping.Wrap,
};

if (Controller.CheckVersion() == 1)
{
    ContentDialog dialog = new ContentDialog
    {
        XamlRoot = this.XamlRoot,
        Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style,
        Title = "Please write something!",
        Content = inputTextBox,
        IsPrimaryButtonEnabled  = false,
        PrimaryButtonText = "Select",
        CloseButtonText = "Cancel",
    };

    inputTextBox.TextChanged  = (s, e) =>
    {
        dialog.IsPrimaryButtonEnabled = (s as TextBox)?.Text.Count() > 0;
    };

#pragma warning restore IDE0090

    // Is the "Select" button clicked?
    if (await dialog.ShowAsync() is ContentDialogResult.Primary)
    {
        var text = inputTextBox.Text;
    }
}
  • Related