ThemeDictionary
- Assembly:
Fluence.Wpf - Namespace:
Fluence.Wpf.Markup
public sealed class ThemeDictionary : ResourceDictionary
A resource dictionary with per-theme value tables, equivalent to WinUI 3 ResourceDictionary.ThemeDictionaries. Populate ThemeDictionaries with ThemeResourceDictionary tables keyed Light, Dark, HighContrast, or Default; the matching table is selected automatically on every theme change and any DynamicResource (or ThemeResource) reference into it re-resolves.
Remarks: Selection picks the table whose ThemeKey matches the resolved theme reported by ResolvedTheme and falls back to the Default table when the exact theme key is absent. Under high contrast, the WinUI polarity keys HighContrastBlack (dark schemes) and HighContrastWhite (light schemes) are tried before the generic HighContrast key; polarity is judged from the live system window luminance, so custom schemes classify by how their background reads. Unlike WinUI, the tables carry their key on the ThemeKey property rather than x:Key: the WPF markup compiler cannot compile keyed children inside a dictionary-typed property of a ResourceDictionary subclass. In XAML:
<Grid.Resources>
<fluence:ThemeDictionary>
<fluence:ThemeDictionary.ThemeDictionaries>
<fluence:ThemeResourceDictionary ThemeKey="Light">
<SolidColorBrush x:Key="HeroBrush" Color="#EEEEEE" />
</fluence:ThemeResourceDictionary>
<fluence:ThemeResourceDictionary ThemeKey="Dark">
<SolidColorBrush x:Key="HeroBrush" Color="#333333" />
</fluence:ThemeResourceDictionary>
</fluence:ThemeDictionary.ThemeDictionaries>
</fluence:ThemeDictionary>
</Grid.Resources>
The type is equally usable from code, which suits values only known at runtime:
ThemeDictionary icons = new()
{
ThemeDictionaries =
{
new ThemeResourceDictionary { ThemeKey = "Light", ["AppIconImageSource"] = lightIcon },
new ThemeResourceDictionary { ThemeKey = "Dark", ["AppIconImageSource"] = darkIcon },
},
};
window.Resources.MergedDictionaries.Add(icons);
Use it in element, window, or application resources. It is not a replacement for the three application-level merged dictionaries owned by ApplicationThemeManager, and its own MergedDictionaries collection is owned by the selection mechanism: entries added there by callers are discarded on the next swap. In a scope WPF seals read-only (Style.Resources, template resources) the dictionary keeps the selection made before sealing instead of swapping. As in WinUI, a StaticResource reference into a theme dictionary does not update after a theme change; reference its keys with DynamicResource or ThemeResourceExtension.
Instances are tracked with weak references, so a discarded ThemeDictionary never leaks through the static theme-change subscription.
Base type: ResourceDictionary (including inherited WPF and .NET members)
Constructors
ThemeDictionary
public ThemeDictionary()
Initializes a new instance of the ThemeDictionary class.
Properties
ThemeDictionaries
public ThemeResourceDictionaryCollection ThemeDictionaries { get; }
Gets the per-theme tables. Each table's ThemeKey names the theme it serves: Light, Dark, HighContrast, or Default (the fallback used when the resolved theme has no exact entry).