Skip to content

Commit 475e8af

Browse files
Fix low-contrast de-emphasized text in WPF Gallery under high contrast (#787)
Dimmed caption/label text (rendered via a hardcoded ~0.7 Opacity over the primary text brush) fell below the 4.5:1 minimum under the Desert contrast theme (MAS 4.3.1), flagged across multiple pages by Accessibility Insights. Route the de-emphasis opacity through a new DynamicResource 'DeemphasizedTextOpacity' and override it to 1.0 in App when SystemParameters.HighContrast is set, so dimmed text renders at full contrast in any contrast theme while staying unchanged in normal mode. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2778df9 commit 475e8af

11 files changed

Lines changed: 45 additions & 25 deletions

File tree

Sample Applications/WPFGallery/App.xaml.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Configuration;
22
using System.Data;
3+
using System.Windows;
34
using Microsoft.Extensions.DependencyInjection;
45
using Microsoft.Extensions.Hosting;
56

@@ -153,9 +154,24 @@ public static void Main()
153154

154155
App app = new();
155156
app.InitializeComponent();
157+
app.ApplyDeemphasizedTextOpacity();
156158
app.MainWindow = _host.Services.GetRequiredService<MainWindow>();
157159
app.MainWindow.Visibility = Visibility.Visible;
158160
app.Run();
159161
}
162+
163+
// Dimmed text fails the 4.5:1 minimum under some high-contrast themes (e.g. Desert), so render
164+
// de-emphasized text at full opacity there. DynamicResource pushes the change to every consumer.
165+
private void ApplyDeemphasizedTextOpacity()
166+
{
167+
Resources["DeemphasizedTextOpacity"] = SystemParameters.HighContrast ? 1.0 : 0.7;
168+
SystemParameters.StaticPropertyChanged += (_, e) =>
169+
{
170+
if (e.PropertyName == nameof(SystemParameters.HighContrast))
171+
{
172+
Resources["DeemphasizedTextOpacity"] = SystemParameters.HighContrast ? 1.0 : 0.7;
173+
}
174+
};
175+
}
160176
}
161177

Sample Applications/WPFGallery/Resources/PageStyles.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
<sys:Double x:Key="TitleLargeTextBlockFontSize">40</sys:Double>
1717
<sys:Double x:Key="DisplayTextBlockFontSize">68</sys:Double>
1818

19+
<!-- Opacity for de-emphasized text; App overrides it to 1.0 under high contrast so dimmed
20+
text keeps the 4.5:1 minimum (e.g. the Desert contrast theme). -->
21+
<sys:Double x:Key="DeemphasizedTextOpacity">0.7</sys:Double>
22+
1923
<Style x:Key="BaseTextBlockStyle" TargetType="TextBlock">
2024
<Setter Property="FontSize" Value="{StaticResource BodyTextBlockFontSize}" />
2125
<Setter Property="FontWeight" Value="SemiBold" />

Sample Applications/WPFGallery/Resources/Templates.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
Width="240"
3939
Margin="10,0,0,0"
4040
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
41-
Opacity="0.7"
41+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
4242
Style="{StaticResource CaptionTextBlockStyle}"
4343
Text="{Binding Description}"/>
4444
</StackPanel>

Sample Applications/WPFGallery/Views/Collections/ListViewPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!--
1+
<!--
22
This Source Code Form is subject to the terms of the MIT License.
33
If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
44
Copyright (C) Leszek Pomianowski and WPF UI Contributors.
@@ -104,7 +104,7 @@
104104
Grid.Column="1"
105105
Margin="12,0,0,6"
106106
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
107-
Opacity="0.7"
107+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
108108
Text="{Binding Company, Mode=OneWay}" />
109109
</Grid>
110110
</DataTemplate>
@@ -115,7 +115,7 @@
115115
MinWidth="120"
116116
Margin="12,0,0,0"
117117
VerticalAlignment="Top">
118-
<Label Foreground="{DynamicResource TextFillColorPrimaryBrush}" Opacity="0.7" Content="Selection mode" Target="{Binding ElementName=SelectionModeComboBox}" />
118+
<Label Foreground="{DynamicResource TextFillColorPrimaryBrush}" Opacity="{DynamicResource DeemphasizedTextOpacity}" Content="Selection mode" Target="{Binding ElementName=SelectionModeComboBox}" />
119119
<ComboBox
120120
x:Name="SelectionModeComboBox"
121121
AutomationProperties.Name="Selection Mode"

Sample Applications/WPFGallery/Views/DesignGuidance/GeometryPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@
5252
<TextBlock
5353
Margin="16,0,0,0"
5454
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
55-
Opacity="0.7"
55+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
5656
Style="{StaticResource CaptionTextBlockStyle}"
5757
Text="Corner radius" />
5858
<TextBlock
5959
Grid.Column="1"
6060
Margin="16,0,0,0"
6161
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
62-
Opacity="0.7"
62+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
6363
Style="{StaticResource CaptionTextBlockStyle}"
6464
Text="Usage" />
6565
<TextBlock
6666
Grid.Column="2"
6767
Margin="16,0,0,0"
6868
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
69-
Opacity="0.7"
69+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
7070
Style="{StaticResource CaptionTextBlockStyle}"
7171
Text="Style" />
7272
</Grid>

Sample Applications/WPFGallery/Views/DesignGuidance/IconsPage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Page x:Class="WPFGallery.Views.IconsPage"
1+
<Page x:Class="WPFGallery.Views.IconsPage"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -22,7 +22,7 @@
2222
<Setter Property="MinHeight" Value="32" />
2323
<Setter Property="Padding" Value="0 0 0 4" />
2424
<Setter Property="Foreground" Value="{DynamicResource TextFillColorPrimaryBrush}" />
25-
<Setter Property="Opacity" Value="0.7"/>
25+
<Setter Property="Opacity" Value="{DynamicResource DeemphasizedTextOpacity}"/>
2626
<Setter Property="Margin" Value="0,4,0,4" />
2727
<Setter Property="FontSize" Value="14"/>
2828
</Style>

Sample Applications/WPFGallery/Views/DesignGuidance/SpacingPage.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@
8383
<TextBlock
8484
Margin="16,0,0,0"
8585
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
86-
Opacity="0.7"
86+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
8787
Style="{StaticResource CaptionTextBlockStyle}"
8888
Text="Value" />
8989
<TextBlock
9090
Grid.Column="2"
9191
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
92-
Opacity="0.7"
92+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
9393
Style="{StaticResource CaptionTextBlockStyle}"
9494
Text="Usage" />
9595
</Grid>

Sample Applications/WPFGallery/Views/DesignGuidance/TypographyPage.xaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Page x:Class="WPFGallery.Views.TypographyPage"
1+
<Page x:Class="WPFGallery.Views.TypographyPage"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -59,25 +59,25 @@
5959
<TextBlock
6060
Margin="16,0,0,0"
6161
Foreground="{StaticResource TextFillColorPrimaryBrush}"
62-
Opacity="0.7"
62+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
6363
Style="{StaticResource CaptionTextBlockStyle}"
6464
Text="Example" />
6565
<TextBlock
6666
Grid.Column="1"
6767
Foreground="{StaticResource TextFillColorPrimaryBrush}"
68-
Opacity="0.7"
68+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
6969
Style="{StaticResource CaptionTextBlockStyle}"
7070
Text="Variable Font" />
7171
<TextBlock
7272
Grid.Column="2"
7373
Foreground="{StaticResource TextFillColorPrimaryBrush}"
74-
Opacity="0.7"
74+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
7575
Style="{StaticResource CaptionTextBlockStyle}"
7676
Text="Size/Line height" />
7777
<TextBlock
7878
Grid.Column="3"
7979
Foreground="{StaticResource TextFillColorPrimaryBrush}"
80-
Opacity="0.7"
80+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
8181
Style="{StaticResource CaptionTextBlockStyle}"
8282
Text="Style" />
8383
</Grid>

Sample Applications/WPFGallery/Views/Samples/UserDashboardPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Page
1+
<Page
22
x:Class="WPFGallery.Views.UserDashboardPage"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -20,7 +20,7 @@
2020
<helpers:EmptyToVisibilityConverter x:Key="EmptyToVisibilityConverter" />
2121

2222
<Style TargetType="Label" x:Key="GenericLabelStyle">
23-
<Setter Property="Opacity" Value="0.67"/>
23+
<Setter Property="Opacity" Value="{DynamicResource DeemphasizedTextOpacity}"/>
2424
<Setter Property="FontWeight" Value="SemiBold"/>
2525
<Setter Property="Foreground" Value="{DynamicResource TextFillColorPrimaryBrush}"/>
2626
</Style>
@@ -91,7 +91,7 @@
9191
Grid.Column="1"
9292
Margin="12,0,0,6"
9393
Foreground="{DynamicResource TextFillColorPrimaryBrush}"
94-
Opacity="0.8"
94+
Opacity="{DynamicResource DeemphasizedTextOpacity}"
9595
Text="{Binding Company, Mode=OneWay}" />
9696
</Grid>
9797
</DataTemplate>

Sample Applications/WPFGallery/Views/SettingsPage.xaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Page
1+
<Page
22
x:Class="WPFGallery.Views.SettingsPage"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -57,7 +57,7 @@
5757
Margin="12"
5858
Orientation="Vertical">
5959
<TextBlock Text="App theme" FontSize="14"/>
60-
<TextBlock Opacity="0.7" FontSize="12" Style="{StaticResource CaptionTextBlockStyle}">Select which app theme to display</TextBlock>
60+
<TextBlock Opacity="{DynamicResource DeemphasizedTextOpacity}" FontSize="12" Style="{StaticResource CaptionTextBlockStyle}">Select which app theme to display</TextBlock>
6161
</StackPanel>
6262

6363

@@ -93,7 +93,7 @@
9393
Margin="12"
9494
Orientation="Vertical">
9595
<TextBlock Text="WPF Gallery" />
96-
<TextBlock Opacity="0.7" Style="{StaticResource CaptionTextBlockStyle}">© 2025 Microsoft. All rights reserved.</TextBlock>
96+
<TextBlock Opacity="{DynamicResource DeemphasizedTextOpacity}" Style="{StaticResource CaptionTextBlockStyle}">© 2025 Microsoft. All rights reserved.</TextBlock>
9797
</StackPanel>
9898
</Grid>
9999
</Expander.Header>

0 commit comments

Comments
 (0)