Skip to content

IAppearanceService

IAppearanceService provides a platform-agnostic way to access the appearance choices supported by the DeltaDesign theme and apply the chosen one. It allows your ViewModel to change the theme variant, color palette, and UI density at runtime without referencing any UI framework type directly.

app-services-appearanceservice-theme-selectors

Main features:

  • Theme variants — Provides access to available theme variants (Light and Dark) and allows you to apply a specific variant.
  • Color palettes — Provides access to the color palettes shipped with the DeltaDesign theme (Nova, Helios, Terra, Vega, Atlas, and so on) and allows you to apply one of these color palettes.
  • UI densities — Provides access to the UI densities available in the DeltaDesign theme (Compact, Standard, and Spacious) and allows you to apply one of these options.
  • Bindable to selectors — The service helps you create selector controls in your UI (such as a ribbon gallery, a combo box, or a menu) to let users switch theme options at runtime.

Interface Definition

public interface IAppearanceService : INotifyPropertyChanged
{
    // A list of available theme variants (light/dark).
    IReadOnlyList<IAppearanceOption> ThemeVariants { get; }

    // A list of available color palettes.
    IReadOnlyList<IAppearanceOption> Palettes { get; }

    // A list of available UI densities.
    IReadOnlyList<IAppearanceOption> Densities { get; }

    // Gets or sets the selected theme variant. 
    IAppearanceOption SelectedThemeVariant { get; set; }

    // Gets or sets the selected theme palette. 
    IAppearanceOption SelectedPalette { get; set; }

    // Gets or sets the selected UI density. 
    IAppearanceOption SelectedDensity { get; set; }
}

IAppearanceOption

Each item in the ThemeVariants, Palettes, and Densities lists implements the IAppearanceOption interface:

public interface IAppearanceOption
{
    // The text shown for this choice.
    string Header { get; }
}

Like the other service interfaces, the IAppearanceOption interface has no references to Avalonia UI framework types. It only contains a string property that specifies the display text for the option. However, actual implementations of the IAppearanceOption interface return richer objects, listed below:

IAppearanceOption Implementation Description
ThemeOption class A light or dark theme variant. Additional properties include:
- ThemeOption.Variant — The theme variant (Avalonia.Styling.ThemeVariant) applied when this option is chosen.
PaletteOption class A color palette. Additional properties include:
- PaletteOption.Palette — The palette (Nova, Helios, Terra, Vega, Atlas, etc.) applied when this option is chosen;
- PaletteOption.Accent — A read-only brush that represents the palette's accent color. The PaletteOption.Accent property helps you show a color swatch in your palette selector, so the user can preview a palette before picking it. See an example below.
DensityOption class A UI density. Additional properties include:
- DensityOption.Density — The density (Compact, Standard, or Spacious) applied when this option is chosen.

The IAppearanceService implementation returns these richer objects. For instance, the actual return value of IAppearanceService.SelectedPalette is a PaletteOption object.

How to Use IAppearanceService

  1. In your ViewModel, expose the IAppearanceService service as a property.
  2. In your View, bind your selector controls to the members of this property.

Access the Service

You need to access the IAppearanceService service in your ViewModel to expose it as a property.

There are two ways to access an IAppearanceService object in a ViewModel:

  • Through the Service<T>() helper method. This is a convenient approach to obtain registered application services shipped with the Eremex Controls library.
  • Through constructor injection.

Service() Helper

Implement a helper Service<T>() method in your ViewModel to get a requested service using the static ApplicationServicesContext.GetRequiredService<T>() method.

using Eremex.AvaloniaUI.Controls.ApplicationServices;
using CommunityToolkit.Mvvm.Input;

public partial class MyViewModel : ObservableObject
{
    // Provides access to any registered service
    protected static T Service<T>() where T : class
        => ApplicationServicesContext.GetRequiredService<T>();

    public IAppearanceService Appearance { get; } = Service<IAppearanceService>();
}

In the App.axaml.cs file, ensure that Eremex application services are registered using SimpleServiceProvider and ApplicationServicesContext as follows:

public class App : Application
{
    public override void OnFrameworkInitializationCompleted()
    {
        RegisterApplicationServices();
        //...
    }

    static void RegisterApplicationServices()
    {
        // Register built-in services.
        var serviceProvider = new SimpleServiceProvider();
        ApplicationServicesContext.RegisterApplicationServices(serviceProvider.AddSingleton);
        ApplicationServicesContext.SetCurrent(serviceProvider);
    }
}

Constructor Injection

Implement a constructor in your ViewModel with IAppearanceService as a parameter. When you instantiate the ViewModel, pass the service object to this constructor.

public partial class MainViewModel : ObservableObject
{
    public MainViewModel(IAppearanceService appearance)
    {
        Appearance = appearance;
    }

    public IAppearanceService Appearance { get; }
}

Expose the Service from the ViewModel

Expose IAppearanceService as a public property. UI controls in the View will bind to it directly.

The following example creates an Appearance property of the IAppearanceService type and initializes it using the Service<T> helper.

public partial class MainViewModel : ObservableObject
{
    public IAppearanceService Appearance { get; }

    public MainViewModel(ThemeVariant? startupThemeVariant = null)
    {
        Appearance = Service<IAppearanceService>();
        Appearance.SelectedThemeVariant = FindTheme(startupThemeVariant);
        //...
    }

    private IAppearanceOption? FindTheme(ThemeVariant? variant)
        => variant is null
            ? null
            : Appearance.ThemeVariants.FirstOrDefault(
                x => string.Equals(x.Header, variant.ToString(), StringComparison.OrdinalIgnoreCase));

    // Provides access to any registered service
    protected static T Service<T>() where T : class
        => ApplicationServicesContext.GetRequiredService<T>();
}

Example - Create a Palette Selector in a View

This example creates a combo box (ComboBoxEditor control) that lists the available theme palettes. When an item is selected in the control's dropdown menu, the corresponding palette is applied to the application.

IAppearanceService - Palette selector example

In this example:

  • ComboBoxEditor.ItemsSource is bound to Appearance.Palettes to list all available palettes in the dropdown menu.
  • ComboBoxEditor.EditorValue is bound to Appearance.SelectedPalette. Picking an item in the control's dropdown applies the corresponding theme palette.
  • The ComboBoxEditor's item template displays the palette's accent color as a swatch next to the palette's name (header).
<mxe:ComboBoxEditor x:Name="themePaletteComboBox"
                    ItemsSource="{Binding Appearance.Palettes}"
                    EditorValue="{Binding Appearance.SelectedPalette}"
                    IsTextEditable="False"
                    ApplyItemTemplateToEditBox="True"
                    Width="150" Margin="0,0,10,0">
    <mxe:ComboBoxEditor.ItemTemplate>
        <DataTemplate x:DataType="appsvc:PaletteOption">
            <StackPanel Orientation="Horizontal" Spacing="8">
                <Border Width="12" Height="12" CornerRadius="2" VerticalAlignment="Center"
                        Background="{Binding Accent}"
                        BorderThickness="1"
                        BorderBrush="{DynamicResource Outline/Neutral/Transparent/Medium}"/>
                <TextBlock Text="{Binding Header}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </mxe:ComboBoxEditor.ItemTemplate>
</mxe:ComboBoxEditor>

See the complete code in the Controls Demo application.

Example - Create a Density Selector

This example creates a combo box (ComboBoxEditor control) populated with UI density options. When a user selects an item, the corresponding layout is automatically applied.

IAppearanceService - UI Density selector example

To show custom images for density values in the combo box, this example populates the control with items directly from the Eremex.AvaloniaUI.Themes.DeltaDesign.Density enumeration. The images are provided using a custom NameToImageConverter converter and displayed using the ComboBoxEditor.ItemTemplate property.

<mxe:ComboBoxEditor x:Name="densityComboBox"
                    ItemsSource="{mxc:EnumItemsSource EnumType=ddt:Density, ShowImages=True,
                                  NameToImageConverter={views:DensityToIconConverter}}"
                    EditorValueChanged="DensityComboBoxEditor_EditorValueChanged"
                    IsTextEditable="False"
                    Margin="0,0,10,0">
    <mxe:ComboBoxEditor.ItemTemplate>
        <DataTemplate x:DataType="mxc:EnumMemberInfo">
            <StackPanel Orientation="Horizontal" Spacing="8">
                <Image Source="{Binding Image}" Stretch="None" VerticalAlignment="Center"/>
                <TextBlock Text="{Binding Name}" VerticalAlignment="Center"/>
            </StackPanel>
        </DataTemplate>
    </mxe:ComboBoxEditor.ItemTemplate>
</mxe:ComboBoxEditor>

See the complete code in the Controls Demo application.

Example - Create a Theme Variant Selector

The following code shows how to build a theme variant selector using SegmentedEditor.

In this example:

  • SegmentedEditor.ItemsSource is bound to the ThemeOptions list defined in a View Model. ThemeOptions returns a list of available theme options (Light and Dark), excluding the default variant (ThemeVariant.Default).
  • SegmentedEditor.EditorValue is bound to Appearance.SelectedThemeVariant. This binding statement applies a corresponding theme variant when the SegmentedEditor's item is selected.
  • The SegmentedEditor.ItemTemplate property is set to an item template that displays each option as a PathIcon.

IAppearanceService - Theme Variant Selector Example

<mxe:SegmentedEditor ItemsSource="{Binding ThemeOptions}" Width="56"
                     EditorValue="{Binding Appearance.SelectedThemeVariant}">
    <mxe:SegmentedEditor.ItemTemplate>
        <DataTemplate x:DataType="appsvc:ThemeOption">
            <PathIcon Width="16" Height="16" Foreground="{DynamicResource Text/Neutral/Secondary}"
                      ToolTip.Tip="{Binding Header}">
                <PathIcon.Data>
                    <MultiBinding Converter="{views:ThemeVariantToIconDataConverter}">
                        <Binding Path="Variant"/>
                        <DynamicResourceExtension ResourceKey="SunPath"/>
                        <DynamicResourceExtension ResourceKey="MoonPath"/>
                    </MultiBinding>
                </PathIcon.Data>
            </PathIcon>
        </DataTemplate>
    </mxe:SegmentedEditor.ItemTemplate>
</mxe:SegmentedEditor>
class MainViewModel 
{
    public IAppearanceService Appearance { get; }

    public IReadOnlyList<IAppearanceOption> ThemeOptions =>
        Appearance.ThemeVariants.Where(x => x is ThemeOption { Variant: not null } option
                                    && option.Variant != ThemeVariant.Default).ToList();
}

See the complete code in the Controls Demo application.

Change Theme Settings in Code

Use the SelectedThemeVariant, SelectedPalette, and SelectedDensity properties to change theme settings in code.

public IAppearanceService Appearance { get; } = Service<IAppearanceService>();

public void ChangeThemeSettings() 
{
    // Apply the 'Terra' palette.
    Appearance.SelectedPalette = Appearance.Palettes.First(p => p.Header == "Terra");  

    Appearance.SelectedDensity = Appearance.Densities[0]; // Switch to the most compact density.

    Appearance.SelectedThemeVariant = Appearance.ThemeVariants[0]; // Switch to the default theme variant.
}