creating-wpf-dialogs

Creating and managing dialog windows for user interaction.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "creating-wpf-dialogs" with this command: npx skills add christian289/dotnet-with-claudecode/christian289-dotnet-with-claudecode-creating-wpf-dialogs

WPF Dialog Patterns

Creating and managing dialog windows for user interaction.

Advanced Patterns: See ADVANCED.md for MVVM dialog service, modeless dialogs, and input dialogs.

  1. Dialog Types Overview

Dialog Types ├── Modal Dialogs (ShowDialog) │ ├── Custom Window dialogs │ └── MessageBox ├── Modeless Dialogs (Show) │ └── Tool windows, floating panels └── Common Dialogs ├── OpenFileDialog ├── SaveFileDialog └── FolderBrowserDialog

  1. MessageBox

2.1 Basic Usage

// Simple message MessageBox.Show("Operation completed successfully.");

// With title MessageBox.Show("File saved.", "Success");

// With buttons var result = MessageBox.Show( "Do you want to save changes?", "Confirm", MessageBoxButton.YesNoCancel);

// With icon MessageBox.Show( "An error occurred.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);

2.2 MessageBox Options

Buttons:

Value Buttons

OK OK

OKCancel OK, Cancel

YesNo Yes, No

YesNoCancel Yes, No, Cancel

Icons:

Value Icon

None No icon

Information Info circle

Warning Warning triangle

Error Red X

2.3 Processing Results

var result = MessageBox.Show( "Are you sure you want to delete this item?", "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No); // Default button

switch (result) { case MessageBoxResult.Yes: DeleteItem(); break; case MessageBoxResult.No: // Cancelled break; }

  1. Custom Modal Dialog

3.1 Dialog Window XAML

<Window x:Class="MyApp.Dialogs.SettingsDialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Settings" Width="400" Height="300" WindowStartupLocation="CenterOwner" ResizeMode="NoResize" ShowInTaskbar="False"> <Grid Margin="20"> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions>

    &#x3C;!-- Content -->
    &#x3C;StackPanel Grid.Row="0">
        &#x3C;Label Content="User Name:"/>
        &#x3C;TextBox x:Name="UserNameTextBox" Margin="0,5,0,15"/>
        &#x3C;CheckBox x:Name="EnableNotificationsCheckBox"
                  Content="Enable notifications"/>
    &#x3C;/StackPanel>

    &#x3C;!-- Dialog buttons -->
    &#x3C;StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
        &#x3C;Button Content="OK" Width="80" Margin="0,0,10,0"
                Click="OkButton_Click" IsDefault="True"/>
        &#x3C;Button Content="Cancel" Width="80"
                Click="CancelButton_Click" IsCancel="True"/>
    &#x3C;/StackPanel>
&#x3C;/Grid>

</Window>

3.2 Dialog Code-Behind

namespace MyApp.Dialogs;

using System.Windows;

public partial class SettingsDialog : Window { public string UserName { get; private set; } = string.Empty; public bool EnableNotifications { get; private set; }

public SettingsDialog()
{
    InitializeComponent();
}

private void OkButton_Click(object sender, RoutedEventArgs e)
{
    // Validate input
    if (string.IsNullOrWhiteSpace(UserNameTextBox.Text))
    {
        MessageBox.Show("Please enter a user name.", "Validation Error",
            MessageBoxButton.OK, MessageBoxImage.Warning);
        UserNameTextBox.Focus();
        return;
    }

    // Set results
    UserName = UserNameTextBox.Text;
    EnableNotifications = EnableNotificationsCheckBox.IsChecked ?? false;

    // Close with success
    DialogResult = true;
}

private void CancelButton_Click(object sender, RoutedEventArgs e)
{
    DialogResult = false;
}

}

3.3 Using the Dialog

private void OpenSettings_Click(object sender, RoutedEventArgs e) { var dialog = new SettingsDialog { Owner = this // Set owner for centering };

if (dialog.ShowDialog() == true)
{
    // User clicked OK
    _currentUserName = dialog.UserName;
    _enableNotifications = dialog.EnableNotifications;
    ApplySettings();
}

}

  1. Common Dialogs

4.1 OpenFileDialog

using Microsoft.Win32;

private void OpenFile_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog { Title = "Open File", Filter = "Text files (.txt)|.txt|All files (.)|.", FilterIndex = 1, InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Multiselect = false, CheckFileExists = true };

if (dialog.ShowDialog() == true)
{
    var filePath = dialog.FileName;
    LoadFile(filePath);
}

}

// Multiple file selection private void OpenMultipleFiles_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFileDialog { Multiselect = true, Filter = "Image files (.png;.jpg)|.png;.jpg" };

if (dialog.ShowDialog() == true)
{
    foreach (var file in dialog.FileNames)
    {
        ProcessFile(file);
    }
}

}

4.2 SaveFileDialog

using Microsoft.Win32;

private void SaveFile_Click(object sender, RoutedEventArgs e) { var dialog = new SaveFileDialog { Title = "Save File", Filter = "Text files (.txt)|.txt|All files (.)|.", FileName = "document.txt", DefaultExt = ".txt", AddExtension = true, OverwritePrompt = true };

if (dialog.ShowDialog() == true)
{
    var filePath = dialog.FileName;
    SaveFile(filePath);
}

}

4.3 .NET 8+ Folder Picker

// .NET 8+ includes OpenFolderDialog private void SelectFolder_Click(object sender, RoutedEventArgs e) { var dialog = new OpenFolderDialog { Title = "Select Output Folder", InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) };

if (dialog.ShowDialog() == true)
{
    var folderPath = dialog.FolderName;
    ProcessFolder(folderPath);
}

}

  1. References
  • Dialog Boxes Overview - Microsoft Docs

  • MessageBox Class - Microsoft Docs

  • OpenFileDialog Class - Microsoft Docs

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

converting-html-css-to-wpf-xaml

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

publishing-wpf-apps

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

using-xaml-property-element-syntax

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

managing-styles-resourcedictionary

No summary provided by upstream source.

Repository SourceNeeds Review