Thursday, December 6, 2007

Quick Application - Tickle Me Pink

colorsI do all my WPF coding in straight XAML, and for my prototypes I like to use the standard .Net colors.

So instead of playing a fun colorname -> color guessing game, I thought I'd have some fun and whip up this tiny color app (it's ridiculously easy in WPF). 

The following code is the entire application, it combines WPF, reflection, Linq and Anonymous Types.

 



UPDATE: Adjusted layout to look better in google reader.

<Window x:Class="Colors.ColorWindow" Title="Colors" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Colors" WindowStyle="ToolWindow"
Height="500" Width="200">
<Window.Resources>
<DataTemplate x:Key="ColorTemplate">
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Black" BorderThickness="1" Width="30" Height="30" Margin="10"
Background="{Binding Brush}" ToolTip="{Binding Brush.Color}" />

<TextBlock Text="{Binding Name}" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</Window.Resources>

<ListBox x:Name="colors" ItemTemplate="{StaticResource ColorTemplate}" />
</Window>
public partial class ColorWindow : Window
{
public ColorWindow()
{
InitializeComponent();

colors.ItemsSource = typeof(Brushes)
.GetProperties(BindingFlags.Static | BindingFlags.Public)
.Select(x => new {
Name = x.Name,
Brush = x.GetValue(null, null)
});
}
}

1 comment:

Anonymous said...

Well written article.