Creating Custom Type Convertors in Silverlight
In this Article we are going to look at Type Convertors and how to translate one CLR type to another. Type Converters are used to convert one .NET Framework CLR type to another type. In silverlight, in a XAML, Type converters are used to convert string representation such as "Test" into their equivalent .NET CLR objects.
Type Converters in Silverlight
In this article we are going to see type converters:
Type Converters are used to convert one .NET Framework CLR type to another type. In silverlight, in a XAML, Type converters are used to convert string representation such as "Test" into their equivalent .NET CLR objects.
Creating Custom Type Converters
Let us see how to convert a string to a Border using Type Converters:
1. To Create Custom type, Create a class say: "BorderTypeConverter". This class should be inherited from the "TypeConverter" class which is available in the System.ComponentModel namespace.
2. Below is the BorderTypeConverter class code.
namespace SilverlightApplication2
{
///
/// Creating a custom type converter" BorderTypeConverter.
///
public class BorderTypeConverter : TypeConverter //TypeConverter base class
{
//context: Provides information about a context to a type converter or a value editor, so that the type converter or editor
//can perform a conversion.
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string); //XAML takes only strings.
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
string val = value as string;
if (val == null) return null;
string[] frag = val.Split(' ');
if (frag.Length < 2)
return null;
//XamlReader.Load function parses a well-formed XAML fragment and creates a corresponding silverlight object tree
//and returns the root of the object tree. Here it creates a SolidColorBrush object.
//Dynamically load XAML to parse a color string for SolidColorBrush.
SolidColorBrush brush1 = (SolidColorBrush)XamlReader.Load("
//Get the Thickness from the frag.
double t;
double.TryParse(frag[1], out t);//Parse thickness.
Thickness tk = new Thickness(t);
//Create and return the Border object
Border border1 = new Border();
border1.BorderBrush = brush1;//Brush used to create the border.
border1.BorderThickness = tk;
return border1;
}
}
Above class has a method "ConvertFrom" which returns the custom Border object. This Border object has the BorderBrush and Thickness properties defined in the class.
Create another class that uses this BorderTypeConverter class to perform custom type conversion.
namespace SilverlightApplication2
{
public class DemoClass : Control
{
[TypeConverter(typeof(BorderTypeConverter))]
public Border Border
{
get {
return (Border)GetValue(BorderProperty);//GetValue method Returns the current effective value of a dependency property "BorderProperty" from a System.Windows.DependencyObject.
}
set {
SetValue(BorderProperty , value );
}
}
//Register a dependency property with the specified property name, property type, owner type, and property metadata for the property.
public static readonly DependencyProperty BorderProperty = DependencyProperty.Register("Border", typeof(Border), typeof(DemoClass), null);
}
}
In the above code The TypeConverter Attribute specifies the type converter "BorderTypeConverter" to use for this "Border" property in this class.
We are also declaring a new Dependency property "BorderProperty" which registers the DependencyProperty with a specific name "Border".
Now let us use the type converter in XAML
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SilverlightApplication2"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White" >
<!--Using the TypeConverter-->
<local:DemoClass Border="Blue 7" Width="150" Height="150"></local:DemoClass>
</Grid>
</UserControl>
In the above code, Border is assigned a value Border="Blue 7" which defines both BorderBrush and the Thickness to the border from the BorderTypeConverter class.
Points to note:
1. Type converters are used to create userdefined types or giving a new representation to the existing types.
2. We have dynamically loaded XAML to create some part of the object tree(SolidColorBrush brush1 in the class BorderTypeConverter) at runtime. This is done using static "Load" method of the XAMLReader class which is from the "System.Windows.Markup" namespace.
You can try this code snippet