Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 493957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:27:12+00:00 2026-05-13T05:27:12+00:00

I’m writing a GUI application where I need to enable editing properties of arbitrary

  • 0

I’m writing a GUI application where I need to enable editing properties of arbitrary objects (their types are only known at run-time).

I’ve decided to use the PropertyGrid control to enable this functionality.
I created the following class:

[TypeConverter(typeof(ExpandableObjectConverter))]
[DefaultPropertyAttribute("Value")]
public class Wrapper
{
    public Wrapper(object val)
    {
        m_Value = val;
    }

    private object m_Value;

    [NotifyParentPropertyAttribute(true)]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    public object Value
    {
        get { return m_Value; }
        set { m_Value = value; }
    }
}

When I get an instance of an object I need to edit, I create a Wrapper for it and set it as the selected object:

Wrapper wrap = new Wrapper(obj);
propertyGrid.SelectedObject = wrap;

But I’ve run into the following problem – the above works as expected only when the type of obj is some custom type (i.e a class that I defined by myself, or a built in complex type) but not when obj is a primitive.

For example, if I define:

[TypeConverter(typeof(ExpandableObjectConverter))]
public class SomeClass
{
    public SomeClass()
    {
        a = 1;
        b = 2;
    }

    public SomeClass(int a, int b)
    {
        this.a = a;
        this.b = b;
    }

    private int a;

    [NotifyParentPropertyAttribute(true)]
    public int A
    {
        get { return a; }
        set { a = value; }
    }

    private int b;

    [NotifyParentPropertyAttribute(true)]
    public int B
    {
        get { return b; }
        set { b = value; }
    }
}

And do:

Wrapper wrap = new Wrapper(new SomeClass());
propertyGrid.SelectedObject = wrap;

Then everything works swell. On the other hand, when I perform the following:

int num = 1;
Wrapper wrap = new Wrapper(num);
propertyGrid.SelectedObject = wrap;

Then I can see the value “1” in the grid (and it’s not grayscaled) but I can’t edit the value. I noticed that if I change Wrapper’s “Value” property’s type to int and remove the TypeConverter attribute, it works.
I get the same behavior for other primitive types and strings.

What is the problem?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:27:12+00:00Added an answer on May 13, 2026 at 5:27 am

    If you set ExpandableObjectConverter to your Value property, it won’t be editable and this is normal because CanConvertFrom will return false. IF you remove the type converter, the PropertyGrid will use the generic TypeConverter and you are again in the same case. So the workaround is to attach a smarter TypeConverter that will act as a wrapper to the correct TypeConverter. Here is a dirty one (I had not much time, you will complete it as needed since I just implemented the ConvertFrom part):

    public class MySmartExpandableObjectConverter : ExpandableObjectConverter
    {
        TypeConverter actualConverter = null;
    
        private void InitConverter(ITypeDescriptorContext context)
        {
            if (actualConverter == null)
            {
                TypeConverter parentConverter = TypeDescriptor.GetConverter(context.Instance);
                PropertyDescriptorCollection coll = parentConverter.GetProperties(context.Instance);
                PropertyDescriptor pd = coll[context.PropertyDescriptor.Name];
    
                if (pd.PropertyType == typeof(object))
                    actualConverter = TypeDescriptor.GetConverter(pd.GetValue(context.Instance));
                else
                    actualConverter = this;
            }
        }
    
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            InitConverter(context);
    
            return actualConverter.CanConvertFrom(context, sourceType);
        }
    
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            InitConverter(context); // I guess it is not needed here
    
            return actualConverter.ConvertFrom(context, culture, value);
        }
    }
    

    Let me know if you need to finetune something.

    Nicolas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.