In This Article

ThemedMessageBox

The ThemedMessageBox class is intentionally designed to be consistent with the native WPF MessageBox API and can be used to quickly display the most common prompts.

Screenshot

ThemedMessageBox dialog with optional status image

Tip

See the Builder Pattern topic for additional details on showing more advanced user prompts.

Showing a ThemedMessageBox

The ThemedMessageBox.Show method is used to show a basic prompt based on the arguments passed. The only required argument is the text to be displayed, so the simplest prompt can be shown as follows:

ThemedMessageBox.Show("Operation complete.");

There are several overloads of the Show method where each accepts a combination of one or more of the following basic arguments:

Argument Description
messageBoxText The primary message text to be displayed.
caption The title bar caption of the window.
button One or more of the MessageBoxButton values for each button to display.
icon One of the MessageBoxImage values indicating a status image to display.
defaultResult The default MessageBoxResult, which must correspond to one of the given button values.

The following code demonstrates prompting the user with a question and storing the result:

result = ThemedMessageBox.Show(
	"The specified file already exists. Do you want to overwrite the file?",
	"Overwrite existing file?",
	MessageBoxButton.YesNo,
	MessageBoxImage.Question
	MessageBoxResult.No);

Owner

The user prompt must have an owner Window. Several of the ThemedMessageBox.Show overloads allow an owner to be specified. If one is not specified, a default owner will be determined by the currently active Window.

Advanced Configuration

Since the ThemedMessageBox API is intentionally designed to be consistent with the native WPF MessageBox API, it only supports basic prompts. By limiting the functionality, it makes ThemedMessageBox easy to use.

For more advanced configurations, it is typically best to use the UserPromptBuilder instead of ThemedMessageBox. In fact, UserPromptBuilder is exactly what ThemedMessageBox.Show uses to display a prompt!

If you still want to use ThemedMessageBox for advanced configurations, there is an optional configure argument to the Show method that allows you to define a callback that will receive the UserPromptBuilder used for the ThemedMessageBox before it is shown.

See the Builder Pattern topic for more details on configuration options, including a global configuration callback is always applied to a message box.

The following demonstrates defining a configure callback to add a header message:

ThemedMessageBox.Show(
	"The project was successfully compiled and deployed to the remote server."
	configure: builder => builder.WithHeaderContent("Deploy successful!")
	);

Replace MessageBox with ThemedMessageBox

The native WPF MessageBox is a convenient and effective component for prompting the user of an application, but the lack of theme support creates an inconsistent experience. This is especially true with applications utilizing a dark theme only to have a MessageBox dialog appear as a bright, white box on top of an otherwise dark canvas.

ThemedMessageBox is a fully themed drop-in replacement for MessageBox that intentionally uses the same arguments for the ThemedMessageBox.Show method. This allows most applications to convert to fully themed prompts by replacing MessageBox with ThemedMessageBox in their existing code.

Note

The MessageBox.Show overloads which accept MessageBoxOptions are not implemented by ThemedMessageBox since the functionality enabled by those options is not necessary for WPF windows. When converting, exclude any arguments for MessageBoxOptions and test your application for expected behavior.

Alias-based Conversion

Since the API is the same between MessageBox and ThemedMessageBox, an application can easily redirect all MessageBox usage to ThemedMessageBox by using an alias.

Tip

Using an alias to convert an application to ThemedMessageBox will help ensure the native MessageBox is not accidentally used out of habit or by unknowing contributors.

The following code demonstrates using an alias in the same file:

using MessageBox = ActiproSoftware.Windows.Controls.ThemedMessageBox;
...
MessageBox.Show("This message will be displayed by ThemedMessageBox without any other code change.");

Starting with C# v10, a global alias can be used to affect all files:

global using MessageBox = ActiproSoftware.Windows.Controls.ThemedMessageBox;
Warning

The alias will not apply to any usage of MessageBox written with a fully or partially qualified domain name; e.g., System.Windows.MessageBox.Show.