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