Click here to Skip to main content
15,867,851 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF/MVVM Application for showing SystemColors list

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
4 Sep 2015CPOL2 min read 21.1K   399   5   8
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.

XML
<Rectangle 
          Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
          Stroke="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>

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:

Image 1

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>}}

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.

Image 2

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:

C#
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));
                }
            }
        }
    }
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseVery good app! Pin
Member 1463713116-Sep-22 17:16
Member 1463713116-Sep-22 17:16 
QuestionNot an article Pin
Herman<T>.Instance9-Sep-15 0:06
Herman<T>.Instance9-Sep-15 0:06 
AnswerRe: Not an article Pin
Igor Sorokin9-Sep-15 11:53
Igor Sorokin9-Sep-15 11:53 
AnswerRe: Not an article Pin
Kornfeld Eliyahu Peter9-Sep-15 19:54
professionalKornfeld Eliyahu Peter9-Sep-15 19:54 
SuggestionNice one... Pin
Kornfeld Eliyahu Peter8-Sep-15 23:36
professionalKornfeld Eliyahu Peter8-Sep-15 23:36 
GeneralRe: Nice one... Pin
Igor Sorokin9-Sep-15 11:45
Igor Sorokin9-Sep-15 11:45 
GeneralMy vote of 5 Pin
mr. Duan7-Sep-15 20:48
professionalmr. Duan7-Sep-15 20:48 
GeneralThanks Pin
Sasha Yakobchuk6-Sep-15 10:20
Sasha Yakobchuk6-Sep-15 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.