TypeConverter Class

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Provides a unified way of converting types of values to other types. TypeConverter typically supports string-to-object conversion, either for property editors in design environments, or to enable XAML usages.

Inheritance Hierarchy

System..::.Object
  System.ComponentModel..::.TypeConverter
    More...

Namespace:  System.ComponentModel
Assembly:  System (in System.dll)

Syntax

Public Class TypeConverter
public class TypeConverter

The TypeConverter type exposes the following members.

Constructors

  Name Description
TypeConverter Initializes a new instance of the TypeConverter class.

Top

Methods

  Name Description
CanConvertFrom(Type) Returns whether the type converter can convert an object from the specified type to the type of this converter.
CanConvertFrom(ITypeDescriptorContext, Type) Returns whether the type converter can convert an object from the specified type to the type of this converter.
CanConvertTo(Type) Returns whether the type converter can convert an object to the specified type.
CanConvertTo(ITypeDescriptorContext, Type) Returns whether the type converter can convert an object to the specified type.
ConvertFrom(Object) Converts from the specified value to the type of this converter.
ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) Converts from the specified value to the intended conversion type of the converter.
ConvertFromString Converts the specified text to an object.
ConvertTo(Object, Type) Converts the specified value object to the specified type.
ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type) Converts the specified value object to the specified type.
ConvertToString Converts the specified object to a string representation.
Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
GetType Gets the Type of the current instance. (Inherited from Object.)
MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Remarks

The most common type of converter is one that converts to and from a text or string representation. The type converter for a class is associated with the class by applying a TypeConverterAttribute in the class definition. Unless this attribute is overridden, all classes that inherit from this class use the same type converter as the base class.

The most common scenario for TypeConverter in XAML is to support setting XAML attribute values and generating object values when XAML is parsed, both forXAML defined classes and custom classes.

Notes to Implementers

To support the string creation syntax of custom types that are specified as attributes in XAML, you should implement CanConvertFrom(ITypeDescriptorContext, Type) in your TypeConverter class.

For Windows Phone, implementing CanConvertTo and ConvertTo is not crucial, because XAML does not support a general serialization mechanism. However, you might still consider providing a basic CanConvertTo and ConvertTo implementation that reverses the ConvertFrom in your TypeConverter conversion. This will produce a more robust and generalized TypeConverter for anyone that specifically calls the ConvertTo method.

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.ComponentModel Namespace

Other Resources

XAML for Windows Phone 8

Inheritance Hierarchy

System..::.Object
  System.ComponentModel..::.TypeConverter
    Microsoft.Phone.Maps.Controls..::.GeoCoordinateCollectionConverter
    Microsoft.Phone.Maps.Controls..::.GeoCoordinateConverter
    Microsoft.Phone.Maps.Controls..::.LocationRectangleConverter
    Microsoft.Phone.Maps.Controls..::.MapPointConverter
    System..::.UriTypeConverter
    System.Windows..::.DurationConverter
    System.Windows.Input..::.InputScopeConverter
    System.Windows.Input..::.InputScopeNameConverter
    System.Windows.Media..::.ImageSourceConverter
    System.Windows..::.NullableBoolConverter
    System.Windows..::.PropertyPathConverter