{"record":{"id":"2b012e7dad0880b7","repo":"HandyOrg/HandyControl","slug":"the-dialog-is-not-a-derived-class-of-the-frameworkelement","errorCode":null,"errorMessage":"The dialog is not a derived class of the FrameworkElement. ","messagePattern":"The dialog is not a derived class of the FrameworkElement\\. ","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia/HandyControl_Avalonia/Tools/Extension/DialogExtension.cs","lineNumber":70,"sourceCode":"            }\n            catch (Exception e)\n            {\n                tcs.TrySetException(e);\n            }\n        }\n    }\n\n    public static Dialog Initialize<TViewModel>(this Dialog dialog, Action<TViewModel> configure)\n    {\n        configure?.Invoke(dialog.GetViewModel<TViewModel>());\n\n        return dialog;\n    }\n\n    public static TViewModel GetViewModel<TViewModel>(this Dialog dialog)\n    {\n        if (!(dialog.Content is Control frameworkElement))\n            throw new InvalidOperationException(\"The dialog is not a derived class of the FrameworkElement. \");\n\n        if (!(frameworkElement.DataContext is TViewModel viewModel))\n            throw new InvalidOperationException($\"The view model of the dialog is not the {typeof(TViewModel)} type or its derived class. \");\n\n        return viewModel;\n    }\n}\n","sourceCodeStart":52,"sourceCodeEnd":78,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Avalonia/HandyControl_Avalonia/Tools/Extension/DialogExtension.cs#L52-L78","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nvar vm = dialog.GetViewModel<MyDialogViewModel>(); // throws for string content\n// after\nif (dialog.Content is Control)\n{\n    var vm = dialog.GetViewModel<MyDialogViewModel>();\n}","handlingStrategy":"type-guard","validationCode":"if (dialog.Content is Control content) { var vm = dialog.GetViewModel<TViewModel>(); }","typeGuard":"static bool HasControlContent(Dialog d) => d.Content is Control;","tryCatchPattern":"try { return dialog.GetViewModel<TViewModel>(); }\ncatch (InvalidOperationException) { return null; }","preventionTips":["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"],"tags":["dialog","mvvm","invalid-operation"],"backgroundTag":"type-mismatch","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}