Click here to Skip to main content
Licence CPOL
First Posted 10 Jul 2008
Views 29,908
Downloads 921
Bookmarked 21 times

WPF Color Palette

By | 10 Jul 2008 | Article
Show WPF brushes in a ListBox.

Introduction

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"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <!-- Context Menu -->
    <ListBox.ContextMenu>
        <ContextMenu>
            <MenuItem Header="Copy Name" Click="CopyName_Click"></MenuItem>
            <MenuItem Header="Copy Hex" Click="CopyHex_Click"></MenuItem>
        </ContextMenu>
    </ListBox.ContextMenu>
    <!-- Item Panel Template just for show items in warp mode -->
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <!-- Items Data Template -->
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="130"/>
                </Grid.ColumnDefinitions>
                <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}" />
                </StackPanel>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

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()
    {
        InitializeComponent();
        // Bind to ListBox
        lsbBrushes.DataContext = _brushes;
    }
    // Copy selected to Clipboard
    private void CopyName_Click(object sender, RoutedEventArgs e)
    {
        if (lsbBrushes.SelectedIndex != -1)
            Clipboard.SetText(((WPFBrush)lsbBrushes.SelectedItem).Name);
    }
    private void CopyHex_Click(object sender, RoutedEventArgs e)
    {
        if (lsbBrushes.SelectedIndex != -1)
            Clipboard.SetText(((WPFBrush)lsbBrushes.SelectedItem).Hex);
    }
}

License

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

About the Author

Sam Farajpour

Software Developer
Home
Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberBastaNL5:16 19 Oct '11  
GeneralMy vote of 4 PinmemberSalam633119:08 11 Oct '11  
QuestionGood Stuff.. PinmemberSalam633119:08 11 Oct '11  
GeneralThank you! Pinmemberthe.komplikator@gmail.com9:36 20 Nov '09  
AnswerRe: Thank you! PinmemberSam Farajpour10:43 20 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Jul 2008
Article Copyright 2008 by Sam Farajpour
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid