HandyOrg/HandyControl · error · InvalidOperationException
The dialog is not a derived class of the FrameworkElement.
Error message
The dialog is not a derived class of the FrameworkElement.
What it means
DialogExtension.GetViewModel requires the Dialog's Content to be a Control (FrameworkElement) so it can read its DataContext. If the dialog content is not a control (e.g. a plain string or view model object), it throws InvalidOperationException.
Solutions
- Ensure the Dialog was shown with a Control-derived view (e.g. a UserControl) whose DataContext is the view model
- Check dialog.Content type before calling GetViewModel, or use the overload appropriate for plain-content dialogs
- Guard with `if (dialog.Content is Control c)` and handle the negative case
Example fix
// before
var vm = dialog.GetViewModel<MyDialogViewModel>(); // throws for string content
// after
if (dialog.Content is Control)
{
var vm = dialog.GetViewModel<MyDialogViewModel>();
} Defensive patterns
Strategy: type-guard
Validate before calling
if (dialog.Content is Control content) { var vm = dialog.GetViewModel<TViewModel>(); } Type guard
static bool HasControlContent(Dialog d) => d.Content is Control;
Try / catch
try { return dialog.GetViewModel<TViewModel>(); }
catch (InvalidOperationException) { return null; } Prevention
- Always host dialogs with Control-derived views
- Set DataContext on the dialog view before calling GetViewModel
- Don't call GetViewModel on plain text/message dialogs
When it happens
Trigger: Calling dialog.GetViewModel<TViewModel>() on a Dialog whose Content was set to a non-Control object, such as Show<string>(...) or a text message dialog.
Common situations: Using HandyControl message dialogs that host plain text instead of a user-control view; calling GetViewModel before the dialog content was assigned; mixing MessageBox-style dialogs with MVVM dialogs.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- The view model of the dialog is not the
- Animation_NoTextChildren
- Calls to BeginInit cannot be nested.
- The JumpList can't be applied until EndInit has been called.
AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14).
Data as JSON: /api/errors/2b012e7dad0880b7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Avalonia/HandyControl_Avalonia/Tools/Extension/DialogExtension.cs:70
}
catch (Exception e)
{
tcs.TrySetException(e);
}
}
}
public static Dialog Initialize<TViewModel>(this Dialog dialog, Action<TViewModel> configure)
{
configure?.Invoke(dialog.GetViewModel<TViewModel>());
return dialog;
}
public static TViewModel GetViewModel<TViewModel>(this Dialog dialog)
{
if (!(dialog.Content is Control frameworkElement))
throw new InvalidOperationException("The dialog is not a derived class of the FrameworkElement. ");
if (!(frameworkElement.DataContext is TViewModel viewModel))
throw new InvalidOperationException($"The view model of the dialog is not the {typeof(TViewModel)} type or its derived class. ");
return viewModel;
}
}
View on GitHub (pinned to 2c0875ebd6)