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

Generic type-guard thrown by DialogExtension.GetViewModel when dialog.Content is not a FrameworkElement. The method needs to reach the view's DataContext to obtain the typed view model, and only a FrameworkElement exposes DataContext — any dialog whose content is a plain object, string, or non-Framework element fails this check before type matching even starts.

Solutions

  1. Ensure the Dialog's Content is set to a FrameworkElement-derived view (UserControl, Window-content control, Page) before calling GetViewModel or Initialize
  2. Set the ViewModel via dialog.DataContext or pass it explicitly if the content cannot be a FrameworkElement
  3. Guard the call site with 'if (dialog.Content is FrameworkElement fe)' and surface a descriptive configuration error instead of letting the exception propagate
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at src/Shared/HandyControl_Shared/Tools/Extension/DialogExtension.cs:70 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/a4b980140f66a3a3. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/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 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)