Sunday, October 14, 2012

How to get list of System Colors in WPF

You might aware about available System Colors in Windows machine. You might have changed system colors using below window. If you change color and appearance of any item (e.g. color, font, size, font color etc.) it will change appearance and color of all the application available in windows. In short you can change system’s default appearance and color using below dialog.

In this post I will demonstrate how you can get available list of system colors dynamically using WPF application. Let’s have a look on below example.

<Window x:Class="WpfApplication1.SysColors"
        Title="System Colors" Height="300" Width="300">
<DataGrid Name="grdSysColorList"
        <DataGridTemplateColumn Header="Color"
                    <TextBlock Margin="5">
                            <SolidColorBrush Color="{Binding Color}" />
        <DataGridTextColumn Header="Name"
                            Binding="{Binding Name}"

public partial class SysColors : Window
public SysColors()
public void LoadSystemColors()
    List<SysColorItem> sysColorList = new List<SysColorItem>();
    Type t = typeof(System.Windows.SystemColors);
    PropertyInfo[] propInfo = t.GetProperties();
    foreach (PropertyInfo p in propInfo)
        if (p.PropertyType == typeof(Color))
            SysColorItem list = new SysColorItem();
            list.Color = (Color)p.GetValue(new Color(),
                BindingFlags.GetProperty, null, null, null);
            list.Name = p.Name;

        else if (p.PropertyType == typeof(SolidColorBrush))
            SysColorItem list = new SysColorItem();
            list.Color = ((SolidColorBrush)p.GetValue(new SolidColorBrush(),
                BindingFlags.GetProperty, null, null, null)).Color;
            list.Name = p.Name;

    grdSysColorList.ItemsSource = sysColorList;

public class SysColorItem
public string Name { get; set; }
public Color Color { get; set; }


In above example you can see DataGrid having two columns Color and Name. Color displays various system colors and name displays its name. I have retrieved this system colors using Reflection. First I have retrieved type of SystemColors and then retrieved all the properties having Color and SolidColorBrush type. In last i have bind that list of SystemColors to DataGrid.

How to apply system colors to WPF controls

In above example we saw how we can retrieve list of system colors dynamically. Now let’s see how you can apply system colors to WPF control.

<Button Background="{x:Static SystemColors.MenuHighlightBrush}"
        Content="Click Me!!!"
        Height="40" Width="150"

Above code demonstrates how you can apply MenuHighlightBrush to Button’s background. Similarly you can apply other system colors as well. SystemColors has all the colors defined as Static property so we have used {x:Static} markup to retrieve system color.

As per above example, if your application running and MenuHighlightBrush color is changed in background then your application will not display newly changed color. If you want to do so you can use DynamicResource markup. DynamicResource will update resources dynamically whenever it changed while your application is running. Let’s have a look on below code snippet which used DynamicResource.

<Button Background="{DynamicResource {x:Static SystemColors.MenuHighlightBrushKey}}"
        Content="Click Me!!!"
        Height="40" Width="150"

In above code I have used DynamicResource markup to display resources dynamically. If MenuHighlightBrushKey changed in background then your application will display newly changed value.

Here one thing to noticed that I have used resource key in second example while in first example I have used brush. The reason behind that is DynamicResource markup uses resource key.

Hope you liked this post. You can checkout below link to know more about System Colors in WPF.

See also –

1 comment:

  1. Awesome..You have clearly explained ...Its very useful for me to know about new things..Keep on blogging..
    Dotnet training in chennai