Before reading this article, I highly recommend reading the previous parts of the series.
In this article, we are going to learn about ContentDialog in Modern C++
ContentDialog is used to create a user-defined Modal Dialog
Modal dialog
It forces the user to get the input (whether success or failure) until user cannot interact with Parent windows. Modal dialog is called child dialog.
Example
VS - Account Settings, then Enter the product key dialog (Modal Dialog), without close this dialog. User cannot interact with the parent dialogue.
ContentDialog
ContentDialog is a modal dialog, it has title, content, default three buttons, primary, secondary and close buttons. This dialog is completely customizable, like all the properties we can assign our own control or styles
- ContentDialog cDlg;
- cDlg.Title(CreateInspectable(L"Content Dialog"));
- cDlg.Content(CreateInspectable(L"C++/WinRT UWP Content Dialog"));
- cDlg.PrimaryButtonText(L"Primary Button");
- cDlg.SecondaryButtonText(L"Secondary Button");
- cDlg.CloseButtonText(L"Close Button");
Show ContentDialog
Show the dialog using the ShowAsync function.
ContentDialogResult
ContentDialog return the ContentDialogResult as a return value
- auto Result = co_await cDlg.ShowAsync();
- ContentDialogResult::Primary -> primary button has clicked
-
- ContentDialogResult::Secondary -> Secondary button has clicked
-
- ContentDialogResult::None -> Close button has clicked
FullSizeDesired
Property used for displaying content dialog in full screen.Complete sample source to handle the ContentDialog
- #include "pch.h"
- using namespace winrt;
- using namespace Windows::ApplicationModel;
- using namespace Windows::ApplicationModel::Activation;
- using namespace Windows::Foundation;
- using namespace Windows::UI;
- using namespace Windows::UI::Xaml;
- using namespace Windows::UI::Xaml::Controls;
- using namespace Windows::UI::Xaml::Controls::Primitives;
- using namespace Windows::UI::Xaml::Interop;
- using namespace Windows::UI::Xaml::Media;
- using namespace Windows::UI::Xaml::Navigation;
- using namespace Windows::UI::Popups;
- using namespace Windows::Storage;
- struct App :ApplicationT<App>
- {
- TextBlock txtStatus;
-
- IInspectable CreateInspectable(hstring strCaption)
- {
- return PropertyValue::CreateString(strCaption);
- }
-
- fire_and_forget BtnClick(IInspectable const & sender, const RoutedEventArgs &args)
- {
- ContentDialog cDlg;
- cDlg.Title(CreateInspectable(L"Content Dialog"));
- cDlg.Content(CreateInspectable(L"C++/WinRT UWP Content Dialog"));
- cDlg.PrimaryButtonText(L"Primary Button");
- cDlg.SecondaryButtonText(L"Secondary Button");
- cDlg.CloseButtonText(L"Close Button");
- cDlg.FullSizeDesired(true);
- auto Result = co_await cDlg.ShowAsync();
-
- switch (Result)
- {
- case ContentDialogResult::Primary:
- txtStatus.Text(L"Primary Clicked");
- break;
- case ContentDialogResult::Secondary:
- txtStatus.Text(L"Secondary Clicked");
- break;
- case ContentDialogResult::None:
- txtStatus.Text(L"None Clicked");
- break;
- default:
- break;
- }
-
- }
-
- TextBlock CreateTextBlock(hstring text)
- {
- TextBlock txtBlock;
- txtBlock.Text(text);
- txtBlock.FontFamily(FontFamily(L"Segoe UI Semibold"));
- txtBlock.TextAlignment(TextAlignment::Left);
- txtBlock.FontSize(35);
- txtBlock.Foreground(SolidColorBrush(Colors::Brown()));
- txtBlock.Margin(CreateThickness(10, 10, 0, 0));
- return txtBlock;
- }
-
- Thickness CreateThickness(int left, int top, int right, int bottom)
- {
- Thickness thick;
- thick.Left = left;
- thick.Top = top;
- thick.Right = right;
- thick.Bottom = bottom;
- return thick;
- }
-
-
- void OnLaunched(LaunchActivatedEventArgs const&)
- {
- Button BtnClick;
- auto captionText = PropertyValue::CreateString(L"Content Dialog");
- BtnClick.Content(captionText);
- BtnClick.Click({ this,&App::BtnClick });
- BtnClick.Margin(CreateThickness(20, 10, 0, 0));
-
- Button BtnCustomClick;
- auto customCaptionText = PropertyValue::CreateString(L"Custom Content Dialog");
- txtStatus = CreateTextBlock(L"Content Status");
- txtStatus.Margin(CreateThickness(20, 10, 0, 0));
- StackPanel sStatusPanel;
- sStatusPanel.Children().Append(txtStatus);
-
- StackPanel sContentPanel;
- sContentPanel.Children().Append(BtnClick);
- sContentPanel.Orientation(Orientation::Horizontal);
- sContentPanel.VerticalAlignment(VerticalAlignment::Top);
-
- StackPanel mainPanel;
- mainPanel.Children().Append(sContentPanel);
- mainPanel.Children().Append(sStatusPanel);
-
-
- Window window = Window::Current();
- window.Content(mainPanel);
- window.Activate();
-
- auto txtNo = CreateTextBlock(L"Welcome");
-
-
- }
- };
-
- int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
- {
- Application::Start([](auto &&) {make<App>(); });
- return 0;
- }
output
So far we have seen the ContentDialog , now I am going to explain , how to customize content dialog
Customize ContentDialog
ContentDialog content property is use to customize the dialog content area, this property is like a container control, prepare the controls you want to use, prepare the list and add it in to panel control, finally assign the panel control to the content property
Ex: below sample code prepares the header text and two checkboxes are added into the ContentDialog
- CheckBox CreateCheckBox(hstring caption,bool IsChecked)
- {
- CheckBox chkBox;
- chkBox.IsChecked(IsChecked);
- chkBox.Content(CreateInspectable(caption));
- return chkBox;
- }
-
- fire_and_forget BtnCustomClick(IInspectable const & sender, const RoutedEventArgs &args)
- {
-
- auto chkBox1 = CreateCheckBox(L"C++/WinRT",true);
- auto chkBox2 = CreateCheckBox(L"C#",false);
- auto header = CreateTextBlock(L"Customize Dialog");
-
- StackPanel sPanel;
- sPanel.Orientation(Orientation::Vertical);
- sPanel.Children().Append(header);
- sPanel.Children().Append(chkBox1);
- sPanel.Children().Append(chkBox2);
-
-
- ContentDialog cDlg;
- cDlg.Title(CreateInspectable(L"Content Dialog"));
- cDlg.Content(sPanel);
- cDlg.CloseButtonText(L"Close");
- auto Result = co_await cDlg.ShowAsync();
- txtStatus.Text(L"Custom Dialog");
- }
Output
All the C++/WinRT samples code
here
Conclusion
I hope you understood how to use ContentDialog control.