wpftoolkit
wpftoolkit copied to clipboard
Edit [Flags] enums using CheckComboBox in PropertyGrid
hpyqwtyb[CodePlex]
e.g. ModifierKey properties should allow multiple choices
Any news on this issue?
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 ?
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()
{
}
}
}
Hi, Thanks for the suggestion. We will add it to our to do list.
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.
Hi, it is not yet available, but we add you as a 2nd user wanting this kind of editor. Thank you for your patience.
Hi
This feature has been added. In PropertyGrid, a new CheckComboBox Editor is now available for Enum properties using "Flags".
Thank you