选择器看起来与CSS非常相似。在Avalonia中有哪些可用的选择器?例如,这个选择器的含义是什么:
<Style Selector="ListBoxItem:selected /template/ ContentPresenter">
<Setter Property="Background" Value="#ffffff" />
</Style>
选择器看起来与CSS非常相似。在Avalonia中有哪些可用的选择器?例如,这个选择器的含义是什么:
<Style Selector="ListBoxItem:selected /template/ ContentPresenter">
<Setter Property="Background" Value="#ffffff" />
</Style>
是的,选择器非常类似于CSS。目前(在0.5.0 alpha版本),以下选择器可用。我会通过示例来解释这些选择器,否则很难理解:
Button
:按类型选择控件,因此如果你编写选择器Button
,它将匹配Avalonia.Controls.Button
类。它不会匹配派生类型[1]:is(Button)
:与 Class
非常相似,除了它还匹配派生类型Button TextBlock
:当两个选择器之间有空格时,选择器将匹配后代元素,因此在这种情况下,选择器将匹配任何TextBlock
,只要它是Button
的逻辑后代Button > TextBlock
:当两个选择器之间有>
时,选择器将匹配子元素,因此在这种情况下,选择器将匹配任何TextBlock
,只要它是Button
的直接逻辑子元素Button#MyButton
:这将匹配任何具有名称为MyButton
的Button
Button.MyClass
:这将匹配任何Button
,其Classes
中包含字符串MyClass
Button:disabled
:这将匹配带有:disabled
伪类的任何Button
Button[IsDefault=true]
: 这将匹配任何具有IsDefault
属性为true
的Button
Button /template/ ContentPresenter
:这将匹配Button
控件模板中的ContentPresenter
[2][1]: 注意,对象的类型实际上是通过查看其IStyleable.StyleKey
属性来确定的,因此如果您希望从Button
继承的控件被设计为Button
,则可以在您的类上实现IStyleable.StyleKey
,并返回typeof(Button)
。
[2]: 这类似于CSS中现已弃用的/deep/
运算符; CSS阴影DOM是与控件模板非常相似的概念。