먼저 Popup 컨트롤을 데모로 보여드리겠습니다
XAML에 버튼을 추가하고 버튼 클릭 시 Popup을 열도록 설정합니다. Popup 요소도 같이 선언하며 기본 상태는 닫혀있고 버튼을 클릭해야 보입니다.
<StackPanel Spacing="24" VerticalAlignment="Top">
<!-- 기본 Popup 루트 컨테이너 -->
<Popup x:Name="MainPopup" Width="320" Height="180">
<Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="8" Padding="16">
<StackPanel Spacing="12">
<TextBlock Text="커스텀 팝업 내용" FontSize="18"/>
<TextBlock Text="순수 기본 Popup 베이스 컨테이너입니다"/>
<Button Content="팝업 닫기" Click="BtnClosePopup_Click"/>
</StackPanel>
</Border>
</Popup>
<Button Content="기본 Popup 컨테이너 열기" Click="BtnOpenPopup_Click" HorizontalAlignment="Left"
ToolTipService.ToolTip="기본 팝업 레이어를 엽니다"/>
</StackPanel>Code language: HTML, XML (xml)
그 다음 버튼 클릭 이벤트 BtnOpenPopup_Click를 생성하고 내부에서 Popup을 표시하는 로직을 작성합니다. <Popup x:Name="MainPopup" Width="320" Height="180"> ...</Popup>는 플로팅 창을 만들며 기본적으로 닫혀있습니다. 안쪽 버튼 <Button Content="팝업 닫기" Click="BtnClosePopup_Click"/>을 클릭하면 해당 Popup이 닫힙니다.
F12 키를 눌러 두 개의 버튼 클릭 이벤트 함수를 자동 생성합니다
.H 헤더 파일
#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
// 전역 네임스페이스 별칭으로 클래스 찾을 수 없음, RoutedEventArgs 정의되지 않음 오류 해결
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++ 컨트롤 강의 – 플로팅 팝업 창 (Popup 알림창)
Previous: WinUI 3 C++ 컨트롤 강의 – 내비게이션 및 탭