Wpf Dialogs Apr 2026
// Save logic
_dialogService = dialogService; SaveCommand = new RelayCommand(_ => Save()); EditItemCommand = new RelayCommand(_ => EditItem());
var windowType = GetWindowTypeForViewModel(viewModel.GetType()); var window = (Window)Activator.CreateInstance(windowType); window.DataContext = viewModel; window.Owner = Application.Current.MainWindow; return window.ShowDialog() == true ? (T)viewModel : null;
private void OkButton_Click(object sender, RoutedEventArgs e) WPF Dialogs
// Basic alert MessageBox.Show("File saved successfully!"); // With title and buttons MessageBoxResult result = MessageBox.Show( "Do you want to save changes?", "Unsaved Changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning);
var dialog = new InputDialog(prompt, defaultText); return dialog.ShowDialog() == true ? dialog.Result : null;
<Window x:Class="MyApp.MyDialog" Title="My Dialog" Height="300" Width="400" WindowStartupLocation="CenterOwner" ShowInTaskbar="False"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <TextBlock Text="Enter your name:" Margin="0,0,0,5"/> <TextBox x:Name="NameTextBox" Grid.Row="1" Margin="0,0,0,10"/> <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,10,0,0"> <Button x:Name="OkButton" Content="OK" Width="75" Margin="0,0,10,0" Click="OkButton_Click"/> <Button x:Name="CancelButton" Content="Cancel" Width="75" Click="CancelButton_Click"/> </StackPanel> </Grid> </Window> // Save logic _dialogService = dialogService; SaveCommand =
private void CancelButton_Click(object sender, RoutedEventArgs e)
private void EditItem()
private Type GetWindowTypeForViewModel(Type viewModelType) // Save logic _dialogService = dialogService
if (dialog.ShowDialog() == DialogResult.OK)
public class DialogService : IDialogService
if (_dialogService.ShowConfirmation("Save changes?", "Confirm")) // Save logic
if (saveFileDialog.ShowDialog() == true)
progressDialog.Close(); // Open File Dialog var openFileDialog = new Microsoft.Win32.OpenFileDialog