[英]WinUI 3.0 Desktop: Background color of TextBox gets changed on mouse over
I have started to work with WinUI 3, the new UI tech from Microsoft.我已经开始使用 WinUI 3,Microsoft 的新 UI 技术。
I have a TextBox
that I want to set to readonly, ie IsReadOnly="True"
(Remark: The user shall still be able to copy content from it, that is why I do not want to set it to disabled, ie IsEnabled="False"
.)我有一个想要设置为只读的TextBox
,即IsReadOnly="True"
(备注:用户仍然可以从中复制内容,这就是我不想将其设置为禁用的原因,即IsEnabled="False"
。)
When setting it to IsReadOnly="True"
I noticed that the TextBox
still keeps its default white background.将其设置为IsReadOnly="True"
我注意到TextBox
仍保持其默认的白色背景。 I wanted to change this, in order to avoid having users clicking on it because they think that it is possible to change its content.我想改变这一点,以避免让用户点击它,因为他们认为可以改变它的内容。
I decided to set some kind of grayish color for the background:我决定为背景设置某种灰色:
<TextBox IsReadOnly="True" Background="SlateGray" Text="The content..."></TextBox>
This works as expected, but with one problem: As soon as the mouse hovers over the TextBox
the background changes back to white .这按预期工作,但有一个问题:只要鼠标悬停在TextBox
,背景就会变回 white 。 When the mouse is no longer over the TextBox
, the color changes back to the specified gray background color.当鼠标不再位于TextBox
上方时,颜色会变回指定的灰色背景色。
In WPF I would have tried some DataTrigger in order to keep the formatting, but there are no DataTriggers (yet?) in WinUI.在 WPF 中,我会尝试一些 DataTrigger 以保持格式,但 WinUI 中没有 DataTriggers(还没有?)。
I also tried setting other color related properties on the TextBox
in order to see if they have anything todo with the mentioned behavior, but setting these did not change anything:我还尝试在TextBox
上设置其他与颜色相关的属性,以查看它们是否与上述行为有任何关系,但设置这些并没有改变任何内容:
FocusVisualPrimaryBrush, FocusVisualSecondaryBrush, SelectionHighlightColor, SelectionHighlightColorWhenNotFocused
You could override some theme resources:您可以覆盖一些主题资源:
<TextBox IsReadOnly="True" Background="SlateGray" Text="The content...">
<TextBox.Resources>
<SolidColorBrush x:Key="TextControlBackgroundPointerOver" Color="SlateGray" />
<SolidColorBrush x:Key="TextControlBackgroundFocused" Color="SlateGray" />
</TextBox.Resources>
</TextBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.