Type converter in XAML

XAML is widely use all .NET Technologies either you are working on WPF , UWP or HoloLens .So to Understand the core basics of XAML is important. In this blog i'm going to show you , how types are converted behind the scenes in XAML. When type some property like color in a string format.

XAML is widely used in most .NET Technologies like WPF , Store Apps ( UWP ) , Microsoft HoloLens , Xamarin etc. So its very important to learn XAML in detail if you want to Write Softwares or Apps like 

  1. Android Apps using C#
  2. iOS Apps using C#
  3. Microsoft HoloLens Apps 
  4. Windows 10 Apps 
XAML is very easy to learn , In this article we'll discuss a very small but Important topic of XAML whitch is "Automatic Type converters in XAML".
 
every programmer use type conversions in day to day programming. But in XAML we perform a specific kind of conversion . To understand Please read the artilce till end.  

Whenever you make some control in XAML you set its properties. For example in following screen shot we are making a button in XAML and setting its properties like:

  • Name
  • Horizontal Alignment
  • Margin
  • Background
  • Vertical Alignment
  • Horizontal Alignment
  • Background Color etc , you are shown a list of given option through Intellisense like below:



So you have limited options as shown in above screenshot. you've to select 1 position from 4 given options . These options are infect enums.

have you ever noticed that when you type in for setting the properties like ,

Now I'm going to show you the alternate way to set properties using C# syntax. To make a button from C# write following code in Page Load Event in program.

When you make a control from C# code. You've to set all of its properties in code. In this case we've set its
  • HorizontalAlignment 
  • Background properties
You selected the background color and Horizontal Alignment from a strongly typed enumeration for the button. So the question is ! How these properties are mapped to strongly typed enumeration, when you type a string which is "left" in the case of alignment property and "Red" in case of color selection.

Answer is ! XAML parser does this job.

XAML parser convert string value into a strongly typed version of that value. So when you set the properties like < horizontalAllignment = "Left" > , the string "Left" will be mapped to the strongly typed enum by the XAML parser.