1

I have a ContentDialog triggered when a user clicks on a row in a DataGrid. The primary goal of the ContentDialog is to allow the user to enter and save a comment via a textbox control in the ContentDialog. When the PrimaryButton is clicked how can I save the contents of the textbox and use it in the calling program? Is it possible or do I need to add a custom button (not use the built-in buttons of the ContentDialog) to perform my save operation from the ContentDialog's own code-behind (before the ContentDialog is closed? Thank you in advance StackOverflow-contributors, you have been so helpful to me during this project. It is greatly appreciated.

My code:

private async void DisplayCommentDialog_v2(Approvals rowModel)
{
    ContentDialog editDialog = new ContentDialog();
    editDialog.Name = "ApprovalContentDialog";
    editDialog.Title = $"Edit this metric: {rowModel.ID}, {rowModel.Market_ID}?";
    editDialog.Title = $"Enter comment for comment id: {rowModel.Comment_ID}?";
    editDialog.Title = $"Enter Management Approval Comment (ID: { rowModel.Comment_ID})";
    editDialog.PrimaryButtonText = "Save";
    //editDialog.IsPrimaryButtonEnabled = false;
    editDialog.SecondaryButtonText = "Clear";
    editDialog.SecondaryButtonClick += EditDialog_SecondaryButtonClick;
    editDialog.CloseButtonText = "Cancel";
    editDialog.DefaultButton = ContentDialogButton.Primary;
    //editDialog.DefaultButton = ContentDialogButton.Close;
    editDialog.Content = new pageApprovalDialog(rowModel.Comment_ID);

    editDialog.XamlRoot = this.Content.XamlRoot;
    ContentDialogResult result = await editDialog.ShowAsync();

    if (result == ContentDialogResult.Primary)
    {
        Save the comment entered by the user in the ContentDialog into the database.  
    }
}

Here is my ContentDialog.content XAML:

<Page
    x:Class="MetricReporting.Pages.pageApprovalDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MetricReporting.Pages"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
    <StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <!-- Content body -->
        <!-- You can add custom content to the dialog window -->
        <TextBlock Text="When a target is not met, a comment from management is required (long and detailed)" TextWrapping="Wrap" />
    
        <TextBox x:Name="Cmnt_Apprvl_Mgmt" PlaceholderText="Enter Comment" Visibility="Visible" Margin="0 10 0 0"/>
    
    </StackPanel>
</Page>
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
LennyL
  • 225
  • 3
  • 9

1 Answers1

1

You can just create a method to get data from the content page.

For example:

ContentDialogPage.xaml

<Page
    x:Class="ContentDialogExample.ContentDialogPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:ContentDialogExample"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">
    <Grid>
        <TextBox x:Name="InputTextBox" />
    </Grid>
</Page>

ContentDialogPage.xaml.cs

using Microsoft.UI.Xaml.Controls;

namespace ContentDialogExample;

public sealed partial class ContentDialogPage : Page
{
    public ContentDialogPage()
    {
        this.InitializeComponent();
    }

    public string GetInputText() => this.InputTextBox.Text;
}

and get the text:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    ContentDialog contentDialog = new()
    {
        XamlRoot = this.Content.XamlRoot,
        IsPrimaryButtonEnabled = true,
        PrimaryButtonText = "OK",
        Content = new ContentDialogPage(),
    };

    if (await contentDialog.ShowAsync() is ContentDialogResult.Primary &&
        contentDialog.Content is ContentDialogPage content)
    {
        string text = content.GetInputText();
    }
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21
  • Thank you so much Andrew! I will try this! Wow, I just discovered your wonderful WinUI 3 series of YouTube videos, and now you help me on StackOverflow. The internet is a wonderful thing. I am so grateful for your help. – LennyL Jun 07 '23 at 21:25
  • Thanks for watching! Glad to be of help! – Andrew KeepCoding Jun 08 '23 at 07:18
  • Dear Andrew, is there a way for me to keep the 'dialog' open, if the user enters no text in the InputTextBox, when the user clicks 'OK', and warn/remind the user that text must be entered? I want to close the dialog when the user clicks 'OK' only if there is text in InputTextBox. – LennyL Jun 12 '23 at 19:44
  • Dear Andrew, is there a way that I can use the built in Secondary button to NOT close the dialog but to instead clear the InputTextBox, and keep the dialog open? – LennyL Jun 12 '23 at 19:49
  • I have read the microsoft documentation but they don't give example of how to 'handle' the built-in buttons: "You may wish to do some work before the dialog closes (for example, to verify that the user entered into form fields before submitting a request). You have two ways to do work before the dialog closes. You can handle the PrimaryButtonClick, SecondaryButtonClick, or CloseButtonClick events to get the user's response when the user presses a button and verify the state of the dialog before it closes. You can also handle the Closing event to do work before the dialog closes." – LennyL Jun 12 '23 at 19:51
  • I guess it's better to create a custom `ContentDialog` for those features. But I guess it's better (recommended) to do this in a new question. Can you post a new question for this? – Andrew KeepCoding Jun 13 '23 at 05:15