This code defines a simple C# WPF application with a main window that contains a button. When the button is clicked, a new instance of the PopupWindow class is created and shown as a modal dialog using the ShowDialog method. The PopupWindow class defines a simple popup window with a text block that displays the message “This is a popup window!“.
Note that the PopupWindow window is defined in a separate XAML file and code-behind file, and is shown as a modal dialog using the ShowDialog method to prevent the user from interacting with the main window while the popup is open.
MainWindow.xaml
<Window x:Class="PopupWindowDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Show Popup" Click="Button_Click"/>
</Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;
namespace PopupWindowDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Create a new instance of the popup window
PopupWindow popupWindow = new PopupWindow();
// Show the popup window
popupWindow.ShowDialog();
}
}
}
PopupWindow.xaml
<Window x:Class="PopupWindowDemo.PopupWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PopupWindow" Height="150" Width="300">
<Grid>
<TextBlock Text="This is a popup window!" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
PopupWindow.xaml.cs
using System.Windows;
namespace PopupWindowDemo
{
public partial class PopupWindow : Window
{
public PopupWindow()
{
InitializeComponent();
}
}
}