Hi experts,
in an application I have a
System.Windows.Forms.PropertyGrid
to let user change some business model properties.
One of these properties is of an enumerated type that is often used similarly, but for a few properties, some values shall not be displayed. Therefore I created
SelectiveEnumAttribute
. It is used in a custom
TypeConverter
's
GetStandardValues()
method to display only the relevant enum values to the user.
This attribute-based selective hiding of enumeration values works in general, but it doesn't when editing multiple objects at the same time.
In either case,
PropertyGrid.SelectedObjects = objectArray
is used.
[Edit2]
Seems my first example has been too narrow. The attributes of interest for me control some of the behaviour of a custom
TypeConverter
. Therefore I need access to a given property's attributes from within the TypeConverter's methods.
blic class MyAttribute : Attribute
{
}
public class MyTypeConverter : EnumConverter
{
public MyTypeConverter(Type type)
: base(type)
{ }
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
int attributeCount = context.PropertyDescriptor.Attributes.Count;
return base.GetStandardValuesSupported(context);
}
}
public class MyBusinessObject
{
public enum AllValues
{
FirstValue,
SecondValue,
ThirdValue,
FourthValue,
FifthValue
}
private AllValues _oneValue = AllValues.FirstValue;
[MyAttribute]
[TypeConverter(typeof(MyTypeConverter))]
public AllValues Value
{
get { return (_oneValue); }
set { _oneValue = value; }
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
MyBusinessObject business1 = new MyBusinessObject();
MyBusinessObject business2 = new MyBusinessObject();
propertyGrid1.SelectedObjects = new object[] { business1, business2 };
}
}
When feeding multiple objects into PropertyGrid, the attributes attached to their properties seem to simply vanish. Even though objects are of the same type and therefore, their properties' attributes are identical (in value and even in existence).
Check this in
MyTypeConverter.GetStandardValuesSupported()
. When showing multiple objects in the grid, the attribute count equals zero.
As long as there is only one object to show, this attribute collection contains the attributes in question and I can access their stored values (not shown in example for simplicity).
[/Edit2]
How can I preserve the attached attribute while multiselecting objects for the PropertyGrid?