//------------------------------------------ // ColorGrid.cs (c) 2006 by Charles Petzold //------------------------------------------ using System; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; namespace Petzold.SelectColorFromGrid { class ColorGrid : ListBox { // The colors to be displayed. Brush[] brushes = new Brush[] { Brushes.Black, Brushes.Brown, Brushes.DarkGreen, Brushes.MidnightBlue, Brushes.Navy, Brushes.DarkBlue, Brushes.Indigo, Brushes.DimGray, Brushes.DarkRed, Brushes.OrangeRed, Brushes.Olive, Brushes.Green, Brushes.Teal, Brushes.Blue, Brushes.SlateGray, Brushes.Gray, Brushes.Red, Brushes.Orange, Brushes.YellowGreen, Brushes.SeaGreen, Brushes.Aqua, Brushes.LightBlue, Brushes.Violet, Brushes.DarkGray, Brushes.Pink, Brushes.Gold, Brushes.Yellow, Brushes.Lime, Brushes.Turquoise, Brushes.SkyBlue, Brushes.Plum, Brushes.LightGray, Brushes.LightPink, Brushes.Tan, Brushes.LightYellow, Brushes.LightGreen, Brushes.LightCyan, Brushes.LightSkyBlue, Brushes.Lavender, Brushes.White }; public ColorGrid() { // Define the ItemsPanel template. FrameworkElementFactory factoryUnigrid = new FrameworkElementFactory(typeof(UniformGrid)); factoryUnigrid.SetValue(UniformGrid.ColumnsProperty, 8); ItemsPanel = new ItemsPanelTemplate(factoryUnigrid); // Create a FrameworkElementFactory based on Rectangle. FrameworkElementFactory factoryRectangle = new FrameworkElementFactory(typeof(Rectangle)); factoryRectangle.SetValue(Rectangle.WidthProperty, 12.0); factoryRectangle.SetValue(Rectangle.HeightProperty, 12.0); factoryRectangle.SetValue(Rectangle.MarginProperty, new Thickness(4)); factoryRectangle.SetBinding(Rectangle.FillProperty, new Binding("")); // Create the DataTemplate for the items. DataTemplate template = new DataTemplate(typeof(Brush)); template.VisualTree = factoryRectangle; ItemTemplate = template; // Set the ItemsSource to the array of Brush objects. ItemsSource = brushes; } } }