Primary APIs:
StyleStylesControlThemeSelectorsThemeVariantThemeVariantScopeResourceDictionaryStyleBase, SetterBaseStyleQuery, StyleQueries, StyleQueryComparisonOperatorIStyleHost, IStyleable, IThemeVariantHost, IGlobalStylesISetterInstance, ISetterValueContainerSizingApplication.Styles
ControlTheme
Style
Styles Collection OperationsStyles is a mutable style collection and a resource provider.
High-value members for runtime composition:
Count, Owner, Resourcesthis[int index]CollectionChanged, OwnerChangedTryGetResource(...)Batch and reordering APIs:
AddRange(...), InsertRange(...)Move(...), MoveRange(...)RemoveAll(...), RemoveRange(...)IndexOf(...), Insert(...), RemoveAt(...)Contains(...), CopyTo(...), GetEnumerator()Use AddRange/InsertRange and MoveRange when applying feature packs to reduce churn vs per-item edits.
var styles = Application.Current!.Styles;
styles.AddRange(new IStyle[]
{
new Style(x => x.OfType<Button>().Class("accent"))
{
Setters =
{
new Setter(Button.FontWeightProperty, FontWeight.Bold)
}
}
});
if (styles.Contains(styles[0]))
styles.Move(0, styles.Count - 1);
Framework-level style contracts used by control/theme infrastructure:
StyleBase.Parent, StyleBase.SettersIGlobalStyles.GlobalStylesAdded, IGlobalStyles.GlobalStylesRemovedIStyleHost, IStyleable, IThemeVariantHostISetterInstance, ISetterValue, SetterBaseSelectors)Typed selector APIs are easier to refactor than long selector strings.
Commonly useful APIs:
Selectors.Is(...), Selectors.Is<T>()Selectors.OfType(...), Selectors.OfType<T>()Selectors.Class(...), Selectors.Name(...)Selectors.Child(), Selectors.Descendant(), Selectors.Template(), Selectors.Nesting()Selectors.Not(...)Selectors.NthChild(...), Selectors.NthLastChild(...)Selectors.Or(...)Selectors.PropertyEquals(...)Container-query helper contracts:
StyleQueryStyleQueryComparisonOperatorStyleQueries.And(params StyleQuery[] queries)StyleQueries.And(IReadOnlyList<StyleQuery> query)var selector = default(Selector?)
.Is<Button>()
.Class("accent")
.Not(x => x.Class("danger"))
.NthChild(step: 2, offset: 1);
var style = new Style(_ => selector!);
Core values and fields:
ThemeVariant.Default, ThemeVariant.Light, ThemeVariant.DarkThemeVariant.InheritVariantThemeVariant and PlatformThemeVariantApp and subtree controls:
Application.RequestedThemeVariantThemeVariantScope.RequestedThemeVariantActualThemeVariantThemeVariantTypeConverter (CanConvertFrom(...), ConvertFrom(...))Use ThemeVariantScope for local overrides and avoid branching style logic in code.
ResourceDictionary features commonly used with styles:
MergedDictionaries, ThemeDictionariesTryGetResource(...)AddDeferred(...) / AddNotSharedDeferred(...)SetItems(...), ContainsKey(...), EnsureCapacity(...)Deferred resource entries are useful when expensive value construction should happen only if a key is requested.
Container sizing attached-property APIs (media-query-like behavior):
Container.SizingPropertyContainer.GetSizing(...)Container.SetSizing(...)ContainerSizing (Normal, Width, Height, WidthAndHeight)XAML include and dynamic-resource APIs:
StyleInclude, ResourceIncludeDynamicResourceExtension (ResourceKey)Guidance:
AddRange for bundle registration.Move/MoveRange when precedence must be explicit.Selectors composition.ThemeDictionaries + DynamicResource.Default mode:
XAML-first usage example:
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyApp.App">
<Application.Resources>
<Color x:Key="AccentColor">#2F6FED</Color>
<SolidColorBrush x:Key="AccentBrush" Color="{DynamicResource AccentColor}" />
</Application.Resources>
<Application.Styles>
<Style Selector="Button.accent">
<Setter Property="Background" Value="{DynamicResource AccentBrush}" />
<Setter Property="Foreground" Value="White" />
</Style>
</Application.Styles>
</Application>
Code-only alternative (on request):
var style = new Style(x => x.OfType<Button>().Class("accent"));
style.Setters.Add(new Setter(Button.BackgroundProperty, Brushes.SteelBlue));
style.Setters.Add(new Setter(Button.ForegroundProperty, Brushes.White));
Application.Current!.Styles.Add(style);