How to use ColorPickerPalette with a PropertyGrid

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.

wpf propertygrid not working.png


How to use the ColorPickerPalette inside of a PropertyGrid? Is there a better example?



1 Reply

SN Sudharsan Narayanan Syncfusion Team January 20, 2022 03:12 PM UTC

Hi Bob,

You can be able to achieve the “Use ColorPickerPalette with a PropertyGrid” requirement by using the Custom Editor of Property Grid that we can achieve to use the ColorPickerPalette in the control. So, please find the sample from the below link,

Sample: https://www.syncfusion.com/downloads/support/forum/172116/ze/PropertyGridCustomEditor1712355753

Please check with the above sample and let us know your concerns. We are happy to assist you.


Regards,
Sudharsan
 


Loader.
Up arrow icon