Selectors
OfType
- XAML
- C#
<Style Selector="Button">
<Style Selector="local|Button">
new Style(x => x.OfType<Button>());
new Style(x => x.OfType(typeof(Button)));
Selects a control by type. The first example above selects the Avalonia.Controls.Button
class. To include a XAML namespace in the type separate the namespace and the type with a |
character.
This selector does not match derived types. For that, use the Is
selector.
Note the type of an object is actually determined by looking at its StyleKey
property. By default this simply returns the type of the current instance, but if, for example, you do want your control which inherits from Button
to be styled as a Button
, then you can override the StyleKeyOverride
property on your class to return typeof(Button)
.
Name
- XAML
- C#
<Style Selector="#myButton">
<Style Selector="Button#myButton">
new Style(x => x.Name("myButton"));
new Style(x => x.OfType<Button>().Name("myButton"));
Selects a control by its Name
property with a #
character.
Class
- XAML
- C#
<Style Selector="Button.large">
<Style Selector="Button.large:focus">
new Style(x => x.OfType<Button>().Class("large"));
new Style(x => x.OfType<Button>().Class("large").Class(":focus"));
Selects a control with the specified style classes. Multiple classes should be separated with a .
character, or a :
character in the case of pseudoclasses. If multiple classes are specified then the control must have all of the requested classes present in order to match.
Is
- XAML
- C#
<Style Selector=":is(Button)">
<Style Selector=":is(local|Button)">