I'm trying to implement a PropertyGrid that has a color setting that is limited to a 6 colors. The ColorPickerPalette seems like it should work but I'm not having success.
I have been experimenting with the sample Property Grid Demos. I modified the Person class found in the example in following path:
\SampleBrowser\propertygrid\Getting Started\Model\Person.cs)
Below are my modifications:
[Editor("SysMediaColor", typeof(MyColorPickerPalette))]
[Editor("ObjectColor", typeof(MyColorPickerPalette))]
public class Person
{
[Display(Order = 7)]
[CategoryAttribute("Additional Info")]
[DisplayNameAttribute("Favorite Color")]
[DescriptionAttribute("Favorite color of the actual person.")]
public Brush FavoriteColor { get; set; }
[Display(Order = 14)]
[CategoryAttribute("Additional Info")]
[DisplayNameAttribute("ObjectColor")]
[DescriptionAttribute("ObjectColor")]
public object ObjectColor { get; set; }
[Display(Order = 15)]
[CategoryAttribute("Additional Info")]
[DisplayNameAttribute("System.Media.Color")]
[DescriptionAttribute("System.Media.Color")]
public Color SysMediaColor { get; set; }
public Person()
{
FavoriteColor = Brushes.Gray;
ObjectColor = Colors.Brown;
SysMediaColor = Colors.Pink;
}
}I added a new class called MyColorPickerPalette that inherits from ColorPickerPalette. I added this class to the CommonEditor.cs file found in the following location:
\SampleBrowser\propertygrid\Editors\CommonEditor.cs
public class MyColorPickerPalette : ColorPickerPalette
{
public MyColorPickerPalette()
{
SetProperties();
InitColors();
}
private void SetProperties()
{
this.SetCustomColors = true;
ThemePanelVisibility = Visibility.Collapsed;
StandardPanelVisibility = Visibility.Collapsed;
MoreColorOptionVisibility = Visibility.Collapsed;
HorizontalAlignment = HorizontalAlignment.Center;
BorderThickness = new Thickness(1);
VerticalAlignment = VerticalAlignment.Top;
}
private void InitColors()
{
this.CustomColorsCollection = new System.Collections.ObjectModel.ObservableCollection<CustomColor>()
{
new CustomColor() { Color = new Color() {R = 112, G = 48, B = 160, A = 255}, ColorName = "Purple" },
new CustomColor() { Color = new Color() {R = 0, G = 32, B = 96, A = 255}, ColorName = "Blue" },
new CustomColor() { Color = new Color() {R = 255, G = 0, B = 0, A = 255}, ColorName = "Red" },
new CustomColor() { Color = new Color() {R = 255, G = 192, B = 0, A = 255}, ColorName = "Yellow" },
new CustomColor() { Color = new Color() {R = 0, G = 176, B = 80, A = 255}, ColorName = "Green" }
};
}
}
When I run the application, the ColorPickerPalette does not appear.
How to use the ColorPickerPalette inside of a PropertyGrid? Is there a better example?