wpftoolkit icon indicating copy to clipboard operation
wpftoolkit copied to clipboard

Edit [Flags] enums using CheckComboBox in PropertyGrid

Open xceedsoftware opened this issue 8 years ago • 7 comments

hpyqwtyb[CodePlex]
e.g. ModifierKey properties should allow multiple choices

xceedsoftware avatar Jul 13 '17 12:07 xceedsoftware

Any news on this issue?

JosephGarrone avatar Feb 07 '18 06:02 JosephGarrone

Hi, Can you explain in details, or submit a sample on what exactly you want to do. Is it an editor of type CheckComboBox where multiple selections can be done ?

XceedBoucherS avatar Feb 22 '18 19:02 XceedBoucherS

I ended up implementing this, however it requires the Editor attribute to be set on all fields/properties that I want to use this editor for. It would be nice to have this implemented as a default editor if the [Flags] attribute is found on an Enum.

Usage:

[Editor(typeof(EnumCheckComboBoxEditor), typeof(EnumCheckComboBoxEditor))]

Editor:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using Xceed.Wpf.Toolkit;
using Xceed.Wpf.Toolkit.PropertyGrid;
using Xceed.Wpf.Toolkit.PropertyGrid.Editors;

namespace <Redacted>.Adapters.Xceed.PropertyGrid.Editors
{
    public class EnumCheckComboBoxEditor : TypeEditor<CheckComboBox>
    {
        protected override void SetValueDependencyProperty()
        {
            ValueProperty = global::Xceed.Wpf.Toolkit.Primitives.Selector.SelectedItemsOverrideProperty;
        }

        protected override CheckComboBox CreateEditor()
        {
            return new PropertyGridEditorCheckComboBox();
        }

        protected override void ResolveValueBinding(PropertyItem propertyItem)
        {
            SetItemsSource(propertyItem);
            base.ResolveValueBinding(propertyItem);
        }

        private void SetItemsSource(PropertyItem propertyItem)
        {
            Editor.ItemsSource = CreateItemsSource(propertyItem);
        }

        protected IEnumerable CreateItemsSource(PropertyItem propertyItem)
        {
            return GetValues(propertyItem.PropertyType);
        }

        private static object[] GetValues(Type enumType)
        {
            List<object> values = new List<object>();

            if (enumType != null)
            {
                var fields = enumType.GetFields().Where(x => x.IsLiteral);
                foreach (FieldInfo field in fields)
                {
                    // Get array of BrowsableAttribute attributes
                    object[] attrs = field.GetCustomAttributes(typeof(BrowsableAttribute), false);

                    if (attrs.Length == 1)
                    {
                        // If attribute exists and its value is false continue to the next field...
                        BrowsableAttribute brAttr = (BrowsableAttribute)attrs[0];
                        if (brAttr.Browsable == false)
                        {
                            continue;
                        }
                    }

                    values.Add(field.GetValue(enumType));
                }
            }

            return values.ToArray();
        }
    }

    public class PropertyGridEditorCheckComboBox : CheckComboBox
    {
        static PropertyGridEditorCheckComboBox()
        {

        }
    }
}

JosephGarrone avatar Feb 27 '18 05:02 JosephGarrone

Hi, Thanks for the suggestion. We will add it to our to do list.

XceedBoucherS avatar Mar 23 '18 14:03 XceedBoucherS

Is there any update on this? The code above doesn't work for me (it doesn't read the flags enum values, nor set them), and I can't find an alternative solution anywhere.

billw2012 avatar May 01 '21 13:05 billw2012

Hi, it is not yet available, but we add you as a 2nd user wanting this kind of editor. Thank you for your patience.

XceedBoucherS avatar May 05 '21 11:05 XceedBoucherS

Hi

This feature has been added. In PropertyGrid, a new CheckComboBox Editor is now available for Enum properties using "Flags".

Thank you

evancekafando avatar Jul 25 '22 17:07 evancekafando