简体   繁体   English

WinUI 3.0 桌面:TextBox 的背景颜色在鼠标悬停时更改

[英]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

I am open for all kinds of suggestions on how to keep the background color when the mouse is over the readonly `TextBox`, thank you. 当鼠标悬停在只读`TextBox`上时,我愿意就如何保持背景颜色提出各种建议,谢谢。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM