简体   繁体   中英

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

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.

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

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM