WinUI 3 C++ Controls Guide – Floating Popup Windows (Popup Notification)

First we demonstrate Popup

In XAML, add a button to open this Popup on click. We also declare a Popup hidden by default, which will only appear after clicking the button.

    <StackPanel Spacing="24" VerticalAlignment="Top">

        <!-- Raw Popup Root Container -->
        <Popup x:Name="MainPopup" Width="320" Height="180">
            <Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="8" Padding="16">
                <StackPanel Spacing="12">
                    <TextBlock Text="Custom Popup Content" FontSize="18"/>
                    <TextBlock Text="This is raw Popup base container"/>
                    <Button Content="Close Popup" Click="BtnClosePopup_Click"/>
                </StackPanel>
            </Border>
        </Popup>

        <Button Content="Open Raw Popup Container" Click="BtnOpenPopup_Click" HorizontalAlignment="Left"
        ToolTipService.ToolTip="Open base raw popup layer"/>

    </StackPanel>Code language: HTML, XML (xml)

Then implement the click event BtnOpenPopup_Click to show the Popup. The tag <Popup x:Name="MainPopup" Width="320" Height="180"> ...</Popup> creates a floating window hidden by default. Inside it there is a button <Button Content="Close Popup" Click="BtnClosePopup_Click"/> that closes the parent Popup when clicked.

Press F12 to auto-generate the two button click event handlers

.H Header File

#pragma once

#include "MainWindow.g.h"

namespace winrt::AppWinui::implementation
{
    struct MainWindow : MainWindowT<MainWindow>
    {
    public:
        MainWindow();

        int32_t MyProperty();
        void MyProperty(int32_t value);

        void BtnOpenPopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
        void BtnClosePopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
    };
}

namespace winrt::AppWinui::factory_implementation
{
    struct MainWindow : MainWindowT<MainWindow, implementation::MainWindow>
    {
    };
}
Code language: C++ (cpp)
#include "pch.h"
#include "MainWindow.xaml.h"
#if __has_include("MainWindow.g.cpp")
#include "MainWindow.g.cpp"
#endif

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Globalization.h>

#include <winrt/Windows.UI.h> //Windows::UI::Colors


// Global namespace aliases to fix missing class / RoutedEventArgs errors
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Microsoft::UI::Xaml;
using namespace Microsoft::UI::Xaml::Controls;
using namespace Microsoft::UI::Xaml::Controls::Primitives;


// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace winrt::AppWinui::implementation
{
    int32_t MainWindow::MyProperty()
    {
        throw hresult_not_implemented();
    }

    void MainWindow::MyProperty(int32_t /* value */)
    {
        throw hresult_not_implemented();
    }

    MainWindow::MainWindow()
    {
        InitializeComponent();
    }
}


void winrt::AppWinui::implementation::MainWindow::BtnOpenPopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    MainPopup().IsOpen(true);
}

void winrt::AppWinui::implementation::MainWindow::BtnClosePopup_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    MainPopup().IsOpen(false);
}
Code language: C++ (cpp)

WinUI 3 C++ Controls Guide – Floating Popup Windows (Popup Notification)

Leave a Reply

Your email address will not be published. Required fields are marked *