如何在Avalonia UI中使用伪类(如focus或pointerover)更改控件样式?

huangapple go评论73阅读模式
英文:

How to change a control style using a pseudo class like focus or pointerover in avalonia ui?

问题

我一直在寻找一种在指针悬停在控件上或控件处于焦点时更改控件样式的方法,类似于以下代码:

<Style Selector="TextBox:focus">
    <Setter Property="Background" Value="Red"/>
</Style>

但出于某种原因,没有发生任何变化,文本框的背景颜色仍然保持不变。

英文:

I've been searching for a way to change a control style when a pointer is over it or when it's in focus something like this

&lt;Style Selector=&quot;TextBox:focus&quot;&gt;
    &lt;Setter Property=&quot;Background&quot; Value=&quot;Red&quot;/&gt;
&lt;/Style&gt; 

but for some reason nothing is happenning or changing

instead the TextBox background stayed in the same color

答案1

得分: 0

根据文档 伪类选择器不覆盖默认样式 进行参考。

修正后的代码应该通过使用 模板选择器 来定位边框。

&lt;Style Selector=&quot;TextBox:focus /template/ Border&quot;&gt;
  &lt;Setter Property=&quot;Background&quot; Value=&quot;Red&quot;/&gt;
&lt;/Style&gt;
英文:

By referring to the Documentation Selector with a pseudoclass doesn't override the default

The corrected code should target Border by using template selector

&lt;Style Selector=&quot;TextBox:focus /template/ Border&quot;&gt;
  &lt;Setter Property=&quot;Background&quot; Value=&quot;Red&quot;/&gt;
&lt;/Style&gt;

huangapple
  • 本文由 发表于 2023年6月12日 04:04:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定