MahApps/MahApps.Metro · error · InvalidOperationException

The inverse Flyout theme only works if the window theme abid

Error message

The inverse Flyout theme only works if the window theme abides the naming convention. See ThemeManager.GetInverseAppTheme for more infos

What it means

Thrown by Flyout.ChangeFlyoutTheme when Theme==FlyoutTheme.Inverse and ThemeManager.GetInverseTheme(windowTheme) returns null. GetInverseTheme derives the opposite base-color theme by name convention: the window theme name must contain 'Light' or 'Dark' so the inverse can be resolved. A null result means the active window theme name does not follow this convention (e.g. a custom theme registered with an unconventional name).

Source

Thrown at src/MahApps.Metro/Controls/Flyout.cs:692

        internal void ChangeFlyoutTheme(ControlzEx.Theming.Theme windowTheme)
        {
            if (windowTheme == ThemeManager.Current.DetectTheme(this.Resources))
            {
                return;
            }

            switch (this.Theme)
            {
                case FlyoutTheme.Adapt:
                    ThemeManager.Current.ApplyThemeResourcesFromTheme(this.Resources, windowTheme);
                    break;

                case FlyoutTheme.Inverse:
                    var inverseTheme = ThemeManager.Current.GetInverseTheme(windowTheme);
                    if (inverseTheme is null)
                    {
                        throw new InvalidOperationException("The inverse Flyout theme only works if the window theme abides the naming convention. " +
                                                            "See ThemeManager.GetInverseAppTheme for more infos");
                    }

                    ThemeManager.Current.ApplyThemeResourcesFromTheme(this.Resources, inverseTheme);
                    break;

                case FlyoutTheme.Dark:
                    var darkTheme = windowTheme.BaseColorScheme == ThemeManager.BaseColorDark ? windowTheme : ThemeManager.Current.GetInverseTheme(windowTheme);
                    if (darkTheme is null)
                    {
                        throw new InvalidOperationException("The Dark Flyout theme only works if the window theme abides the naming convention. " +
                                                            "See ThemeManager.GetInverseAppTheme for more infos");
                    }

                    ThemeManager.Current.ApplyThemeResourcesFromTheme(this.Resources, darkTheme);
                    break;

                case FlyoutTheme.Light:

View on GitHub (pinned to 72099e310b)

Solutions

  1. Register custom themes with names containing 'Light' or 'Dark' (e.g. 'MyAccent.Dark', 'MyAccent.Light') so GetInverseTheme can resolve the counterpart.
  2. Switch the Flyout.Theme from Inverse to Adapt, which uses the window theme directly without needing an inverse.
  3. Use a built-in MahApps theme which already follows the naming convention.
  4. Verify with ThemeManager.Current.GetInverseTheme(theme) at startup and fall back to Adapt if null.

Example fix

// before - custom theme with non-conventional name
var dict = new ResourceDictionary { Source = new Uri("pack://.../MyAccent.xaml") };
ThemeManager.Current.AddTheme(dict); // name has no Light/Dark

// after - provide both Light and Dark variants following the convention
ThemeManager.Current.AddTheme(new ResourceDictionary { Source = new Uri("pack://.../MyAccent.Light.xaml") });
ThemeManager.Current.AddTheme(new ResourceDictionary { Source = new Uri("pack://.../MyAccent.Dark.xaml") });
Defensive patterns

Strategy: validation

Validate before calling

// Resolve the inverse theme at startup; fall back to Adapt if unavailable.
var windowTheme = ThemeManager.Current.DetectTheme(window);
var inverse = windowTheme is null ? null : ThemeManager.Current.GetInverseTheme(windowTheme);

flyout.Theme = inverse is null ? FlyoutTheme.Adapt : FlyoutTheme.Inverse;

Try / catch

try { /* let ChangeFlyoutTheme run with Theme=Inverse */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("inverse Flyout theme"))
{
    flyout.Theme = FlyoutTheme.Adapt; // fallback
}

Prevention

When it happens

Trigger: Setting Flyout.Theme=Inverse on a window whose theme was registered with a name lacking Light/Dark; using a custom app theme added via ThemeManager with a non-conventional name; renaming a built-in theme resource.

Common situations: Registering a custom theme with ThemeManager.Current.AddTheme whose key omits the .Light/.Dark suffix; loading theme resource dictionaries that don't follow the MahApps naming pattern; version upgrades where theme keys changed.

Related errors


AI-assisted analysis of MahApps/MahApps.Metro@72099e310b (2026-08-13). Data as JSON: /api/errors/fc67783871deb248. Report an issue: GitHub.