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

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.

Solutions

  1. Verify the dialog view's DataContext is set to an instance of TViewModel (check XAML binding or the code that showed the dialog)
  2. Cast DataContext explicitly first to diagnose the actual type: dialog.Content is Control c ? c.DataContext?.GetType() : null
  3. Use the correct generic type parameter matching the actual DataContext type

Example fix

// before
var vm = dialog.GetViewModel<LoginViewModel>(); // DataContext is actually RegisterViewModel
// after
var vm = dialog.GetViewModel<RegisterViewModel>();
Defensive patterns

Strategy: type-guard

Validate before calling

if (dialog.Content is Control { DataContext: TViewModel vm }) { /* use vm */ }

Type guard

static bool HasViewModel<TViewModel>(Dialog d) => d.Content is Control { DataContext: TViewModel };

Try / catch

try { return dialog.GetViewModel<TViewModel>(); }
catch (InvalidOperationException) { return default; }

Prevention

When it happens

Trigger: 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.

Common situations: Forgetting DataContext binding in the dialog's XAML; requesting the wrong TViewModel after refactoring; dialog opened with data assigned to properties instead of DataContext.

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


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

Appendix: source

Thrown at src/Avalonia/HandyControl_Avalonia/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 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)