Skip to content

Custom modal and non‐modal dialogs

Mattias Kindborg @FantasticFiasco edited this page Nov 27, 2016 · 10 revisions

Dialogs in WPF that don't inherit from Window are called custom dialogs. These custom dialogs are supported, but in order for DialogService to know how to interact with them, you will have to implement a specific interface.

The IWindow interface

The properties and methods of the interface is a subset of the Window API, and those familiar with WPF should be familiar with their purpose.

/// <summary>
/// Interface describing a window.
/// </summary>
/// <remarks>
/// This interface is intended for use when custom windows, i.e. windows not
/// inheriting from <see cref="Window"/>, should be shown.
/// </remarks>
public interface IWindow
{
    /// <summary>
    /// Gets or sets the data context for an element when it participates in data binding.
    /// </summary>
    object DataContext { get; set; }

    /// <summary>
    /// Gets or sets the dialog result value, which is the value that is returned from
    /// the <see cref="ShowDialog" /> method.
    /// </summary>
    /// <value>
    /// The default is false.
    /// </value>
    bool? DialogResult { get; set; }

    /// <summary>
    /// Gets or sets the <see cref="ContentControl"/> that owns this
    /// <see cref="IWindow"/>.
    /// </summary>
    ContentControl Owner { get; set; }

    /// <summary>
    /// Opens a window and returns only when the newly opened window is closed.
    /// </summary>
    /// <returns>
    /// A <see cref="Nullable{Boolean}"/> value that specifies whether the activity was
    /// accepted (true) or canceled (false). The return value is the value of the
    /// <see cref="DialogResult"/> property before a window closes.
    /// </returns>
    bool? ShowDialog();

    /// <summary>
    /// Opens a window and returns without waiting for the newly opened window to close.
    /// </summary>
    void Show();
}

Showing a modal custom dialog using explicit dialog type syntax

The difference between showing a modal dialog inheriting from Window or a modal custom dialog inheriting from IWindow is that the latter requires you to call IDialogService.ShowCustomDialog<T> instead of IDialogService.ShowDialog<T>.

Showing a modal custom dialog using implicit dialog type syntax

The code required for showing a modal dialog is exactly the same indifferent of whether the dialog is inheriting from Window or IWindow.

Showing a non-modal custom dialog using explicit dialog type syntax

The difference between showing a non-modal dialog inheriting from Window or a non-modal custom dialog inheriting from IWindow is that the latter requires you to call IDialogService.ShowCustom<T> instead of IDialogService.Show<T>.

Showing a non-modal custom dialog using implicit dialog type syntax

The code required for showing a non-modal dialog is exactly the same indifferent of whether the dialog is inheriting from Window or IWindow.