danwilloughby.dev

A simple popup window demo using WSP in c#

2 min read

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

Written by Dan Willoughby

Hey I'm Dan. I'm the solo-founder behind Tellspin which helps dev teams respond to support through Slack. Tellspin takes the guesswork out of who should pay attention to which messages making it easier to focus. You should try out Tellspin and follow me on Twitter