dotnet/wpf · error · InvalidOperationException
SR.CantSetOwnerAfterDialogIsShown
Error message
SR.CantSetOwnerAfterDialogIsShown
What it means
Window.Owner setter throws InvalidOperationException if the window is currently being shown modally via ShowDialog. The owner of a modal dialog cannot be changed while the dialog is displayed because WPF has already established the Win32 owner relationship and modal state. You must set the owner before calling ShowDialog.
Solutions
- Set the Owner property before calling ShowDialog.
- If ownership must change at runtime, close the dialog, change Owner, then ShowDialog again.
- Use WindowStartupLocation and owner setup in the parent right after constructing the dialog.
Example fix
// before
var dlg = new OptionsDialog();
dlg.ShowDialog();
dlg.Owner = mainWindow;
// after
var dlg = new OptionsDialog { Owner = mainWindow };
dlg.ShowDialog(); Defensive patterns
Strategy: validation
Validate before calling
bool canSetOwner(Window w) => w != null && !w.IsLoaded; // set owner before window is loaded/shown modally
Try / catch
try { dlg.Owner = owner; dlg.ShowDialog(); }
catch (InvalidOperationException) { dlg.ShowDialog(); } Prevention
- Always set Owner immediately after constructing the dialog, before ShowDialog.
- Never mutate window ownership from inside dialog event handlers.
- Centralize dialog launching in a helper that wires ownership first.
When it happens
Trigger: Setting window.Owner (or Owner property) while _showingAsDialog is true, i.e. after ShowDialog() has been called on the window and before it closes.
Common situations: Assigning an owner from inside the dialog itself (e.g. in Loaded or a button handler), or a parent window trying to re-parent its modal dialog after launch.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- SR.CannotSetOwnerToItself
- SR.CantSetOwnerToClosedWindow
- SR.CantSetOwnerWhosHwndIsNotCreated
- SR.CircularOwnerChild
- SR.DialogResultMustBeSetAfterShowDialog
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/d20a72dffc1680bc.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:1233
// is not allowed
VerifyApiSupported();
VerifyContextAndObjectState();
return _ownerWindow;
}
set
{
// this call ends up throwing an exception if accessing Owner
// is not allowed
VerifyApiSupported();
VerifyContextAndObjectState();
if (value == this)
{
throw new ArgumentException(SR.CannotSetOwnerToItself);
}
if (_showingAsDialog)
{
throw new InvalidOperationException(SR.CantSetOwnerAfterDialogIsShown);
}
if (value != null && value.IsSourceWindowNull)
{
// Try to be specific in the error message.
if (value._disposed)
{
throw new InvalidOperationException(SR.CantSetOwnerToClosedWindow);
}
throw new InvalidOperationException(SR.CantSetOwnerWhosHwndIsNotCreated);
}
if ( _ownerWindow == value )
{
return;
}
if (!_disposed)View on GitHub (pinned to 81131a70a4)