Hi 1Luc1,
We can achieve the goal. But we need to use TypeConverter for the property instead of using an enum type.
As you may know, if the type of the property is Enum, the property will display a domain list in the property Grid which is via reflection. Therefore, the way you choose may not work. The property grid will always display all items of the Enum Type.
To solve the problem, we can implement a custom TypeConverter, and provide value list based from another property value in the Class. So I changed your code a little, please see it below.
Code Snippet
enum TestType { Item, Operation }
public class Settings
{
private TestType myTestType;
[CategoryAttribute("General Performance Analysis Settings")]
public TestType TestType
{
get
{
return myTestType;
}
set
{
myTestType = value;
}
}
private string test;
[CategoryAttribute("General Performance Analysis Settings")]
[TypeConverter(typeof(TestCollectionConverter))]
public string TestCollection
{
get
{
return test;
}
set
{
test = value;
}
}
public Settings()
{
myTestType = TestType.Item;
}
}
public class TestCollectionConverter : StringConverter
{
public override bool GetStandardValuesSupported(
ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
if(((Settings)context.Instance).TestType == TestType.Item)
{
return new StandardValuesCollection(new string[]{ "AA","BB"});
}
else if(((Settings)context.Instance).TestType== TestType.Operation)
{
return new StandardValuesCollection(new string[]{"CC","DD"});
}
else
{
return new StandardValuesCollection(new string[] { "null" });
}
}
If you have further problems, please feel free to let me know.
Best regards,
Bruce Zhou
|