{"record":{"id":"8fc33dd7b1a8eb92","repo":"HandyOrg/HandyControl","slug":"the-view-model-of-the-dialog-is-not-the-typeof-tviewmodel","errorCode":null,"errorMessage":"The view model of the dialog is not the {typeof(TViewModel)} type or its derived class. ","messagePattern":"The view model of the dialog is not the (.+?) type or its derived class\\. ","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Avalonia/HandyControl_Avalonia/Tools/Extension/DialogExtension.cs","lineNumber":73,"sourceCode":"                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":55,"sourceCodeEnd":78,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Avalonia/HandyControl_Avalonia/Tools/Extension/DialogExtension.cs#L55-L78","documentation":"After confirming the dialog content is a Control, GetViewModel checks that its DataContext is (or derives from) TViewModel. If the DataContext is null or of another type, it throws InvalidOperationException naming the expected type.","triggerScenarios":"Calling dialog.GetViewModel<TViewModel>() when the view's DataContext was never set, or is set to a different view-model type than the requested generic parameter.","commonSituations":"Forgetting DataContext binding in the dialog's XAML; requesting the wrong TViewModel after refactoring; dialog opened with data assigned to properties instead of DataContext.","solutions":["Verify the dialog view's DataContext is set to an instance of TViewModel (check XAML binding or the code that showed the dialog)","Cast DataContext explicitly first to diagnose the actual type: dialog.Content is Control c ? c.DataContext?.GetType() : null","Use the correct generic type parameter matching the actual DataContext type"],"exampleFix":"// before\nvar vm = dialog.GetViewModel<LoginViewModel>(); // DataContext is actually RegisterViewModel\n// after\nvar vm = dialog.GetViewModel<RegisterViewModel>();","handlingStrategy":"type-guard","validationCode":"if (dialog.Content is Control { DataContext: TViewModel vm }) { /* use vm */ }","typeGuard":"static bool HasViewModel<TViewModel>(Dialog d) => d.Content is Control { DataContext: TViewModel };","tryCatchPattern":"try { return dialog.GetViewModel<TViewModel>(); }\ncatch (InvalidOperationException) { return default; }","preventionTips":["Bind DataContext in dialog XAML","Match the generic parameter to the actual view-model type","Assert DataContext type in debug builds before showing the dialog"],"tags":["dialog","mvvm","datacontext"],"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"}