Home > Mobile >  WPF change (override) highlight and border color in all controls
WPF change (override) highlight and border color in all controls

Time:12-07

I researched and tested a lot of code before asking this question.

IDE: VS2022. OS: Windows 11. App: WPF .NET 6 (core) C# T-SQL

I'm trying to override Colors

What am I doing wrong?

Thank you for your help.

Update:

Using a style did not work.

<Style TargetType="PasswordBox">
    <Setter Property="SelectionBrush" Value="Red" />
    <Setter Property="SelectionTextBrush" Value="Red" />
</Style>

Style

CodePudding user response:

TextBox and PasswordBox control their highlight brush via their SelectionBrush property. Not all controls use the SystemColors.HighlightBrushKey. SystemColors.HighlightBrushKey is used by the DataGrid for example, but not by the ListBox.

Use SelectionOpacity to control the opacity of the highlight brush.

To change the SelectionBrush globally you must add a Style to the App.xaml resources:

App.xaml

<ResourceDictionary>
  <Style TargetType="PasswordBox">
    <Setter Property="SelectionBrush" Value="Red" />
    <Setter Property="SelectionOpacity" Value="0.8" />
  </Style>

  <Style TargetType="TextBox">
    <Setter Property="SelectionBrush" Value="Red" />
    <Setter Property="SelectionOpacity" Value="0.8" />
  </Style>
</ResourceDictionary>
  • Related