Windows Develop Bookmark and Share   
 index > Windows Forms Designer > PropertyGrid - Where one property dropdown dependant on another property ...
 

PropertyGrid - Where one property dropdown dependant on another property ...

Hello,

I have a PropertyGrid which looks like this:


TestType is an enumeration:
public enum TestType { Item,Operation }
Collection is an enumeration:
public enum TestCollection { NET, C5, Goletas, PowerCollection }
I "fill" the grid with an Settings Class like this propertyGrid.SelectedObject = new Settings();
Settings.cs
 public class Settings {
private TestType myTestType;
[CategoryAttribute("General Performance Analysis Settings")]
public TestType TestType {
get { return myTestType; }
set { myTestType = value; }
}
private TestCollection myCollectio
n;
[CategoryAttribute("General Performance Analysis Settings")]
public TestCollection Collection {
get { return GetCollections(); }
set { myCollection = value; }
}

public Settings() {
myTestType = TestType.Item;
myCollection = GetCollections();
}

private TestCollection GetCollections() {
switch (myTestType) {
case TestType.Item:
return (TestCollection.C5 | TestCollection.Goletas);
case TestType.Operation:
return TestCollection.NET | TestCollection.PowerCollection;
}
throw new System.NotSupportedException("The Test Type " +
myTestType.ToString() + " is not recognized.");
}
}

Now I want, depending on what I choose in TestType, the selectable Items in the Collection field to change. E.g. In TestType I choose "Item" and in Collection I only can choose "C5" and "Goletas". If I choose "Collection" I only can choose "NET" and "PowerCollection".

I appreciate any help.

Luc
a1Luc1  Thursday, November 06, 2008 8:21 PM

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

Bruce.Zhou  Wednesday, November 12, 2008 8:04 AM

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

Bruce.Zhou  Wednesday, November 12, 2008 8:04 AM

You can use google to search for other answers

Custom Search

More Threads

• Set collection item value through PropertyDescriptor?
• How do I not open multiple child forms within a single MDI Parent form?
• fire form1 event using form2 control
• Visual Studio is not adding a .resx with a form...
• Problem drawing custom object when hidden by windwo, then redrawing.
• Windows Forms : designerCould not be shown. dotnetfx setup messy?
• Adding dynamic panels to a panel in C#
• Derived UserControl with weird error : There is already a command handler for the menu command '5efc7975-14bc-11cf-9b2b-00aa00573819 : 17'
• How to write codes to changes the sound volume
• Finding dynamically created controls in Windows Form