none
String List Typeconverter help RRS feed

  • Question

  • Ok, so I have a property where it's getting/setting a string value. I want to use a type converter to give a me a list of acceptable strings to populate the drop down list with.

    The problem, is that the drop down list is specific to the current item loaded in the property grid and needs to be programmatically generated.

    I know how to make the type converter that will display my list of drop down items, the issue I'm running into is how can I get access to my class data so I can generate my list.

    Thanks.
    Friday, May 4, 2007 2:38 AM

Answers

  • The "context" argument to the TypeConverter methods is available to reference the instance of the object being edited.  Here's an example:

      public class Sample {
        private bool mAnimal;
        private string mName;
        public Sample(bool animal, string name) {
          mAnimal = animal; mName = name;
        }
        public bool Animal {
          get { return mAnimal; }
          set { mAnimal = value; }
        }
        [TypeConverter(typeof(SampleConverter))]
        public string Name {
          get { return mName; }
          set { mName = value; }
        }

        private class SampleConverter : StringConverter {
          public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
            Sample obj = (Sample)context.Instance;
            if (obj.Animal) return new StandardValuesCollection(new string[] { "bat", "cow", "dog" });
            else return new StandardValuesCollection(new string[] { "apple", "banana", "pear" });
          }
          public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
            return true;
          }
          public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
            return true;
          }
        }
      }

    Friday, May 4, 2007 4:20 AM
    Moderator

All replies

  • The "context" argument to the TypeConverter methods is available to reference the instance of the object being edited.  Here's an example:

      public class Sample {
        private bool mAnimal;
        private string mName;
        public Sample(bool animal, string name) {
          mAnimal = animal; mName = name;
        }
        public bool Animal {
          get { return mAnimal; }
          set { mAnimal = value; }
        }
        [TypeConverter(typeof(SampleConverter))]
        public string Name {
          get { return mName; }
          set { mName = value; }
        }

        private class SampleConverter : StringConverter {
          public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) {
            Sample obj = (Sample)context.Instance;
            if (obj.Animal) return new StandardValuesCollection(new string[] { "bat", "cow", "dog" });
            else return new StandardValuesCollection(new string[] { "apple", "banana", "pear" });
          }
          public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) {
            return true;
          }
          public override bool GetStandardValuesSupported(ITypeDescriptorContext context) {
            return true;
          }
        }
      }

    Friday, May 4, 2007 4:20 AM
    Moderator
  • Ahh nice. Thanks man. =)
    Friday, May 4, 2007 5:31 PM