How to Get Brush or Color that System Uses for Selected Controls?

Steve Wood 56 Reputation points

I want to get the Brush or Color that is used in UWP applications for controls that are selected. When a control is selected by the user the system sets the background color to indicate that the control is selected and while it's possible to flash a control on the screen and set focus to it then get the background brush and color I'm hoping there's a way to get the brush or color that doesn't make it look like there's a glitch in the app. I'm also hoping that there's a way to get the other brushes and colors that are used by the system, for example the highlight brush and disabled (grayed out) brush.



  Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Loaded="Page_Loaded">  
  <RelativePanel x:Name="thisRelativePanel">  
   private void Page_Loaded(object sender, RoutedEventArgs e)  
    TextBlock thisUnSelectedTextBlock = new TextBlock();  
    thisUnSelectedTextBlock.Text = "thisUnSelectedTextBlock";  
    Border thisUnSelectedBorder = new Border();  
    thisUnSelectedBorder.Child = thisUnSelectedTextBlock;  
    TextBlock thisSelectedTextBlock = new TextBlock();  
    thisSelectedTextBlock.Text = "thisSelectedTextBlock";  
    Border thisSelectedBorder = new Border();  
    thisSelectedBorder.Child = thisSelectedTextBlock;  
    Brush thisSelectedBrush = (Brush)App.Current.Resources["SystemControlHighlightListAccentLowBrush"];  
    thisSelectedBorder.Background = thisSelectedBrush;  
    RelativePanel.SetBelow(thisSelectedBorder, thisUnSelectedBorder);  
    ListBox thisListBox = new ListBox();  
    thisListBox.SelectedIndex = 1;  
    RelativePanel.SetBelow(thisListBox, thisSelectedBorder);  
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. fourelem 76 Reputation points

    Hello. Though I think it may be possible to achive the color seen in selected ListBoxItem's background by using SystemControlHighlightListAccentLowBrush background in a container with SystemControlBackgroundChromeMediumLowBrush background, but it's not enough?


            <RowDefinition Height="Auto"/>  
        <Border Background="{ThemeResource SystemControlBackgroundChromeMediumLowBrush}" Margin="8">  
            <StackPanel Margin="4">  
                    <Style TargetType="Border">  
                        <Setter Property="Margin" Value="4"/>  
                        <Setter Property="Padding" Value="4"/>  
                <TextBlock Margin="8">Background=SystemControlBackgroundChromeMediumLowBrush (ListBox.Background)</TextBlock>  
                <Border Background="{ThemeResource SystemControlHighlightListAccentHighBrush}">  
                    <TextBlock>SystemControlHighlightListAccentHighBrush (SelectedPressed)</TextBlock>  
                <Border Background="{ThemeResource SystemControlHighlightListAccentLowBrush}">  
                    <TextBlock>SystemControlHighlightListAccentLowBrush (Selected)</TextBlock>  
                <Border Background="{ThemeResource SystemControlHighlightListAccentMediumBrush}">  
                    <TextBlock>SystemControlHighlightListAccentMediumBrush (SelectedPointerOver)</TextBlock>  
                <Border Background="{ThemeResource SystemControlHighlightListMediumBrush}">  
                    <TextBlock>SystemControlHighlightListMediumBrush (Pressed)</TextBlock>  
                <Border Background="{ThemeResource SystemControlHighlightListLowBrush}">  
                    <TextBlock>SystemControlHighlightListLowBrush (PointerOver)</TextBlock>  
        <ListBox Grid.Row="1" SelectedIndex="0" Margin="8">  
            <ListBoxItem IsEnabled="False">Disabled</ListBoxItem>  

    Or otherwise, if you'd like to obtain the mixed color in the code behind, interpolate two brushes based on Opacity.

    public SolidColorBrush ListBoxSelectedItemBackgroundBrush  
            // Let's get SystemAccentColor with 0.4 opacity on ListBox's background color.  
            // <SolidColorBrush x:Key="SystemControlHighlightListAccentLowBrush" Color="{ThemeResource SystemAccentColor}" Opacity="0.4" />  
            var fg_brush = Resources.ThemeDictionaries["SystemControlHighlightListAccentLowBrush"] as SolidColorBrush;  
            // <SolidColorBrush x:Key="SystemControlBackgroundChromeMediumLowBrush" Color="{StaticResource SystemChromeMediumLowColor}" />  
            var bg_brush = Resources.ThemeDictionaries["SystemControlBackgroundChromeMediumLowBrush"] as SolidColorBrush; // #FFF2F2F2 in Light theme  
            SolidColorBrush mixed_brush = new SolidColorBrush(Color.FromArgb(  
                (byte)(bg_brush.Color.A * (1.0 - fg_brush.Opacity) + fg_brush.Color.A * fg_brush.Opacity),  
                (byte)(bg_brush.Color.R * (1.0 - fg_brush.Opacity) + fg_brush.Color.R * fg_brush.Opacity),  
                (byte)(bg_brush.Color.G * (1.0 - fg_brush.Opacity) + fg_brush.Color.G * fg_brush.Opacity),  
                (byte)(bg_brush.Color.B * (1.0 - fg_brush.Opacity) + fg_brush.Color.B * fg_brush.Opacity)  
            ); // #FF91C1E7   
            return mixed_brush;  

2 additional answers

Sort by: Most helpful
  1. Groovykool 236 Reputation points

    Do you want to capture the colors from a live app or do just want to know which colors a specific UWP control uses in the latest version of windows?

    Color is not static and depends on Theme.

    Start here.

  2. Richard Zhang-MSFT 6,936 Reputation points


    Welcome to our Microsoft Q&A platform!

    Your problem is how to get the system's default brush resources.

    All the default resources used by UWP are stored in the file generic.xaml. You can access this file in two ways:

    1. Access via file path

    Suppose the drive where you installed the Windows 10 SDK is C:\, then the generic.xaml file path is

    C:\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\{system_version}\Generic.

    2. Jump through Visual Studio

    Place the cursor on the default resource reference in the project (such as the Background resource referenced in the newly created Page), and press F12 to jump to the corresponding resource entry in generic.xaml.


    After entering generic.xaml, we can find the default style of the required control.

    Suppose we need to modify the background color of the Button when the pointer is moved over. Through generic.xaml, we know that the background color of the Button in PointerOver resource name is ButtonBackgroundPointerOver, then we can rewrite it in App.xaml.

            <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Red"/>  

    In this way, when you move the pointer into the Button, you will find that the control turns red.

    Through the above method, you can find the brush resources used by almost all the controls and modify them by rewriting in the project.

    But regarding the highlight brush when selecting the control, this is another problem, you can refer to this document: Reveal Focus.



    When you know the key name, use this method to get the corresponding brush resource:

    var brush = (Brush)Application.Current.Resources["ButtonBackgroundPointerOver"];  

    When you assign values to the corresponding properties of the control, pay attention to the type matching.