Search code examples
wpfxamltextblocksystemcolors

Assign a SystemColor foreground to a TextBlock on XAML for WPF


I'm trying to assign some SystemColor foreground to a TextBlock on XAML for WPF. The main idea is to make the app more accessible, i.e. to be seen nicely also with High Contrast.

I've tried the following code, but it doesn't have any effect on the color displayed:

  <StackPanel Orientation="Horizontal"                        
        Background="{DynamicResource {x:Static SystemColors.HotTrackBrush}}">
        <TextBlock Text="my app" 
                   HorizontalAlignment="Left"                                               
                   Style="{StaticResource MainPageText}"
                   VerticalAlignment="Center"
                   TextWrapping="Wrap"
                   x:Name="Title"/>
  </StackPanel>

I'm pretty new at this, so could you please tell me what you think is the problem?

thanks in advance.


Solution

  • To receive dynamic change notification you need to use HotTrackBrushKey.

    <StackPanel Orientation="Horizontal"                         
                Background="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}">
    

    Without dynamic change notification

    <StackPanel Orientation="Horizontal"
                Background="{x:Static SystemColors.HotTrackBrush}">
    

    For a reference see: SystemColors Reference