Search code examples
c#wpftypeconverter

How can I find out, whether a certain type has a string converter attached?


Here is the problem:

I have a property of a certain object. This property is of Type t. I need to find out, if a string value can be attached to this property.

For example: I have an instance of a Windows.Controls.Button. I need a mechanism, that will return true for property Button.Background, but false for Button.Template.

Can anybody help? Thanks a lot


Solution

  • I think you take the problem in the wrong direction :

    The property does not accepts directly String: actually the property is converted to the good type if a converter exists.

    You then may look if a converter exist using this code :

    public static bool PropertyCheck(Type theTypeOfTheAimedProperty, string aString)
    {
       // Checks to see if the value passed is valid.
       return TypeDescriptor.GetConverter(typeof(theTypeOfTheAimedProperty))
                .IsValid(aString);
    }
    

    These pages may interest you too :

    1. http://msdn.microsoft.com/en-us/library/aa970913.aspx
    2. http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx