MaterialDesignInXAML/MaterialDesignInXamlToolkit · error · InvalidOperationException
Content cannot be passed to a dialog via the OpenDialog if D
Error message
Content cannot be passed to a dialog via the OpenDialog if DialogContent already has a binding.
What it means
AssertTargetableContent rejects passing content via the OpenDialog/Show APIs when DialogContentProperty already has a WPF Binding expression. The host cannot overwrite a bound DialogContent without breaking the binding, so ShowInternal calls AssertTargetableContent early (DialogHost.cs:265) and the check at :798 throws if a BindingExpression exists.
Source
Thrown at src/MaterialDesignThemes.Wpf/DialogHost.cs:797
/// <summary>
/// Callback fired when the <see cref="DialogClosed"/> event is fired, allowing the event to be processed from a binding/view model.
/// </summary>
public DialogClosedEventHandler? DialogClosedCallback
{
get => (DialogClosedEventHandler?)GetValue(DialogClosedCallbackProperty);
set => SetValue(DialogClosedCallbackProperty, value);
}
protected void OnDialogClosed(DialogClosedEventArgs eventArgs)
=> RaiseEvent(eventArgs);
#endregion
internal void AssertTargetableContent()
{
var existingBinding = BindingOperations.GetBindingExpression(this, DialogContentProperty);
if (existingBinding != null)
throw new InvalidOperationException(
"Content cannot be passed to a dialog via the OpenDialog if DialogContent already has a binding.");
}
internal void InternalClose(object? parameter)
{
var currentSession = CurrentSession ?? throw new InvalidOperationException($"{nameof(DialogHost)} does not have a current session");
currentSession.CloseParameter = parameter;
currentSession.IsEnded = true;
//multiple ways of calling back that the dialog is closing:
// * routed event
// * the attached property (which should be applied to the button which opened the dialog
// * straight forward IsOpen dependency property
// * handler provided to the async show method
var dialogClosingEventArgs = new DialogClosingEventArgs(currentSession, DialogClosingEvent);
OnDialogClosing(dialogClosingEventArgs);
_attachedDialogClosingEventHandler?.Invoke(this, dialogClosingEventArgs);View on GitHub (pinned to 98edec3a0b)
Solutions
- Remove the Binding on DialogContent in XAML and pass content via Show(content) / Show(content, identifier) instead.
- If the binding is required, do not pass content to Show; rely on the bound view model and just toggle IsOpen or call Show(null).
- Audit XAML for 'DialogContent=' bindings and reconcile with the chosen open-dialog pattern.
Example fix
<!-- before -->
<materialDesign:DialogHost DialogContent="{Binding MyContent}" />
<!-- after (use imperative Show) -->
<materialDesign:DialogHost Identifier="RootDialog" />
// await DialogHost.Show(new MyView(), "RootDialog"); Defensive patterns
Strategy: validation
Validate before calling
var binding = BindingOperations.GetBindingExpression(dialogHost, DialogHost.DialogContentProperty);
if (binding is not null)
throw new InvalidOperationException("Remove the DialogContent binding before using Show(content).");
await dialogHost.Show(content); Prevention
- Pick ONE dialog pattern: either bind DialogContent in XAML or pass content via Show—not both.
- Audit XAML for 'DialogContent=' bindings before switching to imperative Show.
- If a binding is needed, call Show(null) and toggle IsOpen instead of passing content.
When it happens
Trigger: XAML binds DialogContent (e.g. DialogContent="{Binding ...}") and code then calls Show(content), ShowInternal(content, ...) or DialogSession.UpdateContent(content) with a non-null content object.
Common situations: Designer/data-template bound DialogContent while app code tries to inject a runtime control; migrating from IsOpen binding pattern to async Show pattern without removing the XAML binding; partial views where both a binding and imperative Show are wired.
Related errors
- Unable to find a DialogHost in visual tree
- Unable to find a DialogHost with identifier '{dialogIdentifi
- Unable to find a DialogHost in visual tree ancestry
- Unable to find a DialogHost in visual tree ancestry with ide
- DialogHost is already open.
AI-assisted analysis of MaterialDesignInXAML/MaterialDesignInXamlToolkit@98edec3a0b (2026-08-13).
Data as JSON: /api/errors/e7f289b1011e1bb2.
Report an issue: GitHub.