In This Article

Theme Definitions

A theme definition contains many options that guide a theme generator on how to dynamically generate theme resources. Options cover a variety of theme aspects, including:

  • Font families and base font size.
  • Color ramps for accent and other semantic use scenarios (e.g., success, danger).
  • Default appearance kind when there are several control theme variations (e.g., outline, solid) available for a control type, such as for buttons, switches, edit controls, etc.
  • Corner radii, lengths, paddings, and margins.
Tip

The Getting Started topic covers how to set a ThemeDefinition instance onto a ModernTheme instance.

Theme Definition Options

Theme definitions are represented by the ThemeDefinition class, which has a wide variety of properties for directing theme generator output.

General Options

Property Description
UserInterfaceDensity The density of the user interface, which generally describes the amount of spacing to apply. See the User Interface Density topic for extensive detail on how user interface density works.

Font Options

Property Description
BaseFontSize The font size that is used as a basis for building font size ramps.
DefaultFontFamily The FontFamily used by default.
HeadingFontFamily The FontFamily used by headings.
CodeFontFamily The FontFamily used for code listings, typically a monospace font.

Color Ramp Options

Color ramp name properties should be set to Hue enumeration value names.

Property Description
NeutralColorRampName The name of the neutral (e.g., grayscale) color ramp in the color palette.
AccentColorRampName The name of the accent color ramp in the color palette.
InformationColorRampName The name of the information color ramp in the color palette.
SuccessColorRampName The name of the success color ramp in the color palette.
WarningColorRampName The name of the warning color ramp in the color palette.
DangerColorRampName The name of the danger color ramp in the color palette.
Property Description
MenuItemIconColumnWidth The width for menu item icon columns.
MenuItemPopupColumnWidth The width for menu item popup columns.

Switch Control Options

Property Description
UseAccentedSwitches Whether to use the theme accent color for toggled switch controls.
CheckBoxAppearanceKind The SwitchAppearanceKind that indicates the default appearance for CheckBox controls.
CheckBoxCornerRadius The corner radius for checkboxes.
RadioButtonAppearanceKind The SwitchAppearanceKind that indicates the default appearance for RadioButton controls.
SliderAppearanceKind The SwitchAppearanceKind that indicates the default appearance for Slider controls.
SwitchBorderWidth The border width for checkbox and radio button controls.
SwitchScale The scale factor for checkbox and radio button controls.
ToggleSwitchAppearanceKind The SwitchAppearanceKind that indicates the default appearance for ToggleSwitch controls.
ToggleSwitchHasFarAffinity Whether to use far affinity for ToggleSwitch controls, which will align the track/knob to the far side of the control (e.g., track/knob on right side in left-to-right cultures).

ScrollBar Options

Property Description
ScrollBarThumbMarginAscent The ascent for scroll bar thumb margins, which is the margin space around the short end of the thumb.
ScrollBarThumbMarginExtent The extent for scroll bar thumb margins, which is the margin space around the long end of the thumb.
ScrollBarThumbMaxAscent The maximum ascent for scroll bar thumbs, which is the maximum thickness of the thumb given the current thumb margin settings.
ScrollBarThumbMinAscent The minimum ascent for scroll bar thumbs, which is the minimum thickness of the thumb given the current thumb margin settings.
Tip

Set the ScrollBarThumbMinAscent and ScrollBarThumbMaxAscent properties to the same value, such as 4.0, to have a fixed thumb thickness. Or increase the ScrollBarThumbMaxAscent property to something larger and use the ScrollBarThumbMarginAscent property to indicate how much to indent the thumb from the track's current size.

Other Control Options

Property Description
BadgeAppearanceKind The BadgeAppearanceKind that indicates the default appearance for Badge controls.
ButtonAppearanceKind The ButtonAppearanceKind that indicates the default appearance for various button controls (e.g., Button, SplitButton).
CardAppearanceKind The CardAppearanceKind that indicates the default appearance for Card controls.
EditAppearanceKind The EditAppearanceKind that indicates the default appearance for various edit controls (e.g., ComboBox, TextBox).
SegmentedBarAppearanceKind The SegmentedBarAppearanceKind that indicates the default appearance for Segmented Bar controls.
SpinnerHasHorizontalOrientation Whether to arrange ButtonSpinner buttons horizontally.
TabAppearanceKind The TabAppearanceKind that indicates the default appearance for various tab controls (e.g., TabControl).

Custom Theme Generators

The default theme generator implementation fully generates theme resources for all supported theme variants. Sometimes you may wish to further customize the default output of specific resource values for each theme variant. In those cases, creation of a custom ThemeGenerator-based class is possible. An instance of the custom generator class can be assigned to the ThemeDefinition.Generator property.

See the Theme Generator topic for detail on how to create and install custom theme generator logic, and when it is appropriate to do so.

Creating Derived Classes

It's perfectly fine to create a new instance of the ThemeDefinition class and set its various properties before passing the theme definition to a ModernTheme, as described in the Getting Started topic.

<Application ...
	xmlns:actipro="http://schemas.actiprosoftware.com/avaloniaui"
	xmlns:generation="using:ActiproSoftware.UI.Avalonia.Themes.Generation">
	<Application.Styles>

		<actipro:ModernTheme>
			<actipro:ModernTheme.Definition>
				<generation:ThemeDefinition UseAccentedSwitches="True" AccentColorRampName="Indigo" />
			</actipro:ModernTheme.Definition>
		</actipro:ModernTheme>

	</Application.Styles>
</Application>

If you configure multiple properties on the theme definition, or if you wish to swap in alternate theme definitions at runtime, you may wish to consider creating a custom class that inherits ThemeDefinition and sets all of the properties in the constructor.

Here is a custom MyThemeDefinition class that demonstrates this concept:

public class MyThemeDefinition : ThemeDefinition {

	public MyThemeDefinition() {
		UseAccentedSwitch = true;
		AccentColorRampName = Hue.Indigo.ToString();
	}

}

And here is how a MyThemeDefinition instance can be used:

<Application ... xmlns:actipro="http://schemas.actiprosoftware.com/avaloniaui">
	<Application.Styles>

		<actipro:ModernTheme>
			<actipro:ModernTheme.Definition>
				<local:MyThemeDefinition />
			</actipro:ModernTheme.Definition>
		</actipro:ModernTheme>

	</Application.Styles>
</Application>

Changing Theme Definitions

Actipro's themes include light and dark theme variants in the same theme. Therefore, toggling between light and dark modes is simply a factor of updating the Application.RequestedThemeVariant property as described in the Getting Started topic, which does not require an alternate theme definition to be used.

If your application would like to support a different theme appearance in general (e.g., different colors, font size, spacing), that is where creating alternate theme definitions is required.

Let's assume you have created two ThemeDefinition-based classes named MyThemeDefinition and MySecondThemeDefinition. MyThemeDefinition could have been initialized with code as in the previous section.

Here is a custom MySecondThemeDefinition class that provides an alternate theme appearance with orange accents:

public class MySecondThemeDefinition : ThemeDefinition {

	public MySecondThemeDefinition() {
		AccentColorRampName = Hue.Orange.ToString();
	}

}

The following example shows how the ModernTheme.Definition property can be updated at runtime to a new theme definition, which will immediately update the application appearance.

if (ModernTheme.TryGetCurrent(out var theme))
	theme.Definition = new MySecondThemeDefinition();