TypeConverter Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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
System.UriTypeConverter
System.Windows.Controls.DataGridLengthConverter
System.Windows.Controls.DateTimeTypeConverter
System.Windows.DurationConverter
System.Windows.Media.ImageSourceConverter
System.Windows.NullableBoolConverter
System.Windows.PropertyPathConverter
Namespace: System.ComponentModel
Assembly: System (in System.dll)
Syntax
'Declaration
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 Silverlight is to support setting XAML attribute values and generating object values when XAML is parsed, both for Silverlight defined classes and custom classes. See TypeConverters and XAML.
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) and ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) in your TypeConverter class.
For Silverlight 4, implementing CanConvertTo and ConvertTo is not crucial, because Silverlight 4 XAML does not support a general serialization mechanism. However, you might still consider providing a basic CanConvertTo and ConvertTo implementation that reverses the ConvertFrom(ITypeDescriptorContext, CultureInfo, Object) in your TypeConverter conversion. This will produce a more robust and generalized TypeConverter for anyone that specifically calls the ConvertTo method.
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
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.