Creating a form with a propertyGrid and then adding this code:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace TestPropertyGrid { public class ParentToMyString { public MyString myString { get { return mMyString; } set { mMyString = value; } } MyString mMyString; public ParentToMyString(double aGUID, string text) { mMyString = new MyString(aGUID, text); } } public class MyStringTypeConverter : ExpandableObjectConverter { // public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) // { // if (destinationType == typeof(System.String) && value is MyString) // { // MyString myString = (MyString)value; // return myString.ToString(); // } // return base.ConvertTo(context, culture, value, destinationType); // } public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return false; } public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { // Gather all the localized strings currently loaded List<MyString> myStringCollection = new List<MyString>(); // Gather all the strintTables from the current project. for (int i = 0; i < 5; i++ ) { myStringCollection.Add( new MyString(100 + i,"Test " + i) ); } return new StandardValuesCollection(myStringCollection); } } [TypeConverter(typeof(MyStringTypeConverter))] public class MyString { public double GUID{ get{ return mGUID;} set{ mGUID = value;} } public String Text{ get{ return mText;} set{ mText = value;} } double mGUID; String mText; public MyString(double aGUID,string text) { mGUID = aGUID; mText = text; } public override string ToString() { return (mGUID + " " + mText); } } public partial class Form1 : Form { public Form1() { InitializeComponent(); propertyGrid1.SelectedObject = new ParentToMyString(746387564756, "TestString"); } } }
Will cause a "Object of type 'System.String' cannot be converted to type 'TestPropertyGrid.MyString" when selecting a "MyString" from the dropdown list in the "MyString" property of the grid. Why is this. Is there no way to get an "Object" from the dropdown list even though "MyString" objects is what I am adding?
I want to do this becasuse I want to list the object in the drop down list by a string that might not be "unique". I want to hide the "GUID" from the user as it means nothing to them.
So my list should look like:
"Test 1"
"Test 2"
"Test 3"
Etc..
No GUID anywhere. I am showing the GUID in the example for reference.
Does any one have any thoughts for me?