Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF/MVVM Application for showing SystemColors list

0.00/5 (No votes)
4 Sep 2015 1  
In this article I would like to describe simple WPF application for showing System.Windows.SystemColors list.

Introduction

Software developer of WPF application needs to set color of the XAML element. The color can be determined in the XAML code by the diffirent ways: hardcoded value, static resources, binding to View Modal property, etc. Good practice is to use Dynamic Resources. You can create setter values that refer to system properties, as exposed by SystemColors, SystemFonts, or SystemParameters. These values are truly dynamic because they ultimately come from the runtime environment of the user and operating system.

The following example shows how the DynamicResources can be used in XAML.

<Rectangle 
          Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
          Stroke="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
Colourised in 1ms

If you want to see how a SystemColors name (for example, SystemColors.ControlBrushKey) is presented on your screen, then you can use the tool.

Using the tool

The tool is just single-one .NET Application which should be started on Windows OS. The application is based on .NET Framework 4.0 Client Profile. If you have no the Framework on your PC, then your can installed it from Microsoft site.

The main window of the tool is presented on the following picture:

List of the colors includes 3 columns: color bar, XEH-value of the color (includes alpha chanel value) and SystemColors name. Selected list item shows a "Copy" button. If you click on the button, then the tool creates XAML-string like

{DynamicResource {x:Static <SystemColor name>}}
Colourised in 0ms

You can select color which should be set to your XAML-code, click on the button and paste the stringto XAML.

Using the code

The application is based on MVVM pattern.

MainViewModel class is used as ViewModel of MainWindow.  SysColorViewModal presents List item data. The MainViewModel and SysColorViewModal are inhereted from ViewModelBase class which implements INotifyPropertyChanged interface.

Constructor of the MainViewModel class has the following implementation:

public MainViewModel()
{
    var type = typeof(System.Windows.SystemColors);
    var members = type.GetMembers();
    foreach (var member in members)
    {
        var name = member.Name;           
        if(member.MemberType == MemberTypes.Method)
        {
            if (name.Contains("get_"))
            {
                var method = type.GetMethod(name);
                var result = method.Invoke(null, new object[] {});
                var col = result as SolidColorBrush;
                if(col != null)
                {
                    m_Colors.Add(new SysColorViewModal(name.Substring(4), col, this));
                }
            }
        }
    }
}
Colourised in 22ms

The implementation takes all members of type of SystemColors. If member of the type is MemberType.Method, then the one is method which can be used for getting a correspondinig color. Name of the method is combination of "get_" prefix + name of the system color.

GitHub

The project has a GitHub repository available on this GitHub page.

Any questions, remarks, and comments are welcome.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here