Home > Mobile >  WinUI 3.0 Desktop: Background color of TextBox gets changed on mouse over
WinUI 3.0 Desktop: Background color of TextBox gets changed on mouse over

Time:11-04

I have started to work with WinUI 3, the new UI tech from Microsoft.

I have a TextBox that I want to set to readonly, i.e. 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, i.e. IsEnabled="False".)

When setting it to IsReadOnly="True" I noticed that the TextBox still keeps its default white background. 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. When the mouse is no longer over the TextBox, the color changes back to the specified gray background color.

In WPF I would have tried some DataTrigger in order to keep the formatting, but there are no DataTriggers (yet?) in WinUI.

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:

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.

CodePudding user response:

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>
  • Related