Click here to Skip to main content
13,194,231 members (47,295 online)
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 10 Jul 2008

WPF Color Palette

, 10 Jul 2008
Rate this:
Please Sign up or sign in to vote.
Show WPF brushes in a ListBox.


This simple application lists brushes in System.Windows.Media.Brushes in a ListBox by using Reflection and the WPF Data Binding features.

Using the code

First, add a ListBox and set a DataTemplate in the ItemTemplate attribute:

<!-- Don't forget to set ItemsSource as {Binding} -->
<ListBox Name="lsbBrushes" ItemsSource="{Binding}" Margin="10"

    <!-- Context Menu -->
            <MenuItem Header="Copy Name" Click="CopyName_Click"></MenuItem>
            <MenuItem Header="Copy Hex" Click="CopyHex_Click"></MenuItem>
    <!-- Item Panel Template just for show items in warp mode -->
            <WrapPanel />
    <!-- Items Data Template -->
                    <ColumnDefinition />
                    <ColumnDefinition Width="130"/>
                <Rectangle Fill="{Binding Path=Name}" 

                    Stroke="Black" Margin="5"

                    StrokeThickness="1" Height="50" Width="100"/>
                <StackPanel Grid.Column="1">
                    <Label Content="{Binding Path=Name}" />
                    <Label Content="{Binding Path=Hex}" />

And now, write the binding class and item class like this:

// Binding Class
class WPFBrushList : List<WPFBrush>
    public WPFBrushList()
        // Get type of the Brushes
        Type BrushesType = typeof(Brushes);
        // Get properties of this type
        PropertyInfo[] brushesProperty = BrushesType.GetProperties();
        // Extract Name and Hex code and add to list (binding class)
        foreach (PropertyInfo property in brushesProperty)
            BrushConverter brushConverter = new BrushConverter();
            Brush brush = (Brush)brushConverter.ConvertFromString(property.Name);
            Add(new WPFBrush(property.Name, brush.ToString()));

// Item Class
class WPFBrush
    public WPFBrush(string name, string hex) 
        Name = name;
        Hex = hex;
    //please note name of properties are same as DataTemplate Binding Paths 
    public string Name { get; set; }
    public string Hex { get; set; }

At last, set the binding class to ListBox.DataContext:

public partial class Window1 : Window
    // Create binding object
    private WPFBrushList _brushes = new WPFBrushList();
    public Window1()
        // Bind to ListBox
        lsbBrushes.DataContext = _brushes;
    // Copy selected to Clipboard
    private void CopyName_Click(object sender, RoutedEventArgs e)
        if (lsbBrushes.SelectedIndex != -1)
    private void CopyHex_Click(object sender, RoutedEventArgs e)
        if (lsbBrushes.SelectedIndex != -1)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Sam Farajpour Ghamari
Software Developer Home
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
BastaNL19-Oct-11 5:16
memberBastaNL19-Oct-11 5:16 
GeneralMy vote of 4 Pin
Salam633111-Oct-11 19:08
memberSalam633111-Oct-11 19:08 
QuestionGood Stuff.. Pin
Salam633111-Oct-11 19:08
memberSalam633111-Oct-11 19:08 
GeneralThank you! Pin
the.komplikator@gmail.com20-Nov-09 9:36
memberthe.komplikator@gmail.com20-Nov-09 9:36 
AnswerRe: Thank you! Pin
Sam Farajpour20-Nov-09 10:43
memberSam Farajpour20-Nov-09 10:43 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171018.2 | Last Updated 10 Jul 2008
Article Copyright 2008 by Sam Farajpour Ghamari
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid