Skip to main content

Instance Styles

Instance styles are single-element overrides for style properties. When applied they have the highest precedence and will win over non instance styles.

An instance style is assigned either using the style:PropertyName = expression syntax within a template file or with the C# api from ElementReference.style. The value expression can be either a properly typed expression that matches the property type or can use backticks to use the style file syntax.


// assigning from a template context

template Thing : SomeCompanion {

style:BackgroundColor = `red`;

style:PaddingLeft = $companion.ComputePadding();

style:PreferredWidth = new UIMeasurement(10f, UIMeasurementUnit.Content);

}

// assigning from C#

ElementReference elementRef = SomeHowGetAnElementReference();
elementRef.style.SetPreferredWidth(new UIMeasurement(10f, UIMeasurementUnit.Content));
elementRef.style.SetBackgroundColor(Color.blue);

Assigning instance styles is more expensive and uses more memory than using pre-baked styles. It's not a huge cost but generally you should prefer to use styles over instance styles.