HandyOrg/HandyControl · error · InvalidOperationException

The view model of the dialog is not the

Error message

The view model of the dialog is not the {typeof(TViewModel)} type or its derived class. 

What it means

Generic type-guard thrown by DialogExtension.GetViewModel when the view's DataContext does exist but is not of type TViewModel (nor a subclass). The FrameworkElement check has already passed; the failure is purely a mismatch between the requested generic type and the view's actual DataContext — typically a wrong generic argument in Initialize<TViewModel> or a view wired to a different view model class.

Solutions

  1. Match the generic argument to the view's actual DataContext type, e.g. dialog.Initialize<MyActualViewModel>(vm => ...)
  2. Set the view's DataContext explicitly to the intended TViewModel instance (or use the dialog's ViewModel property) before calling Initialize/GetViewModel
  3. At the call site, check 'if (frameworkElement.DataContext is TViewModel vm)' first and report a clear configuration error instead of relying on the throw
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/Shared/HandyControl_Shared/Tools/Extension/DialogExtension.cs:73 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/85eb04bea41607f1. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Tools/Extension/DialogExtension.cs:73

                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 FrameworkElement 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)