- C# XAML Introduction
- C# XAML Syntax
- C# XAML Layouts
- C# XAML Content and Controls
- C# XAML Introduction Part 3 Type Converters
- C# XAML Introduction Part 4 Object Names Part 1
- C# XAML Combo Boxes
Consider the following C# code that creates a button.
System.Windows.Controls.Button b = new System.Windows.Controls.Button(); b.Content = "OKcode"; b.Background = System.Windows.Media.Brushes.White;
The XAML parser or compiler must look for a type converter that knows how to convert the string ‘white’ into the desired data type. Consider the following XAML that creates several buttons. All these buttons are essentially the same.
<Window x:Class="Ch02TypeConverters.MainWindow"
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:Ch02TypeConverters"
mc:Ignorable="d"
Title="Ch02 WPF 4.5 Unleashed" Height="200" Width="325">
<StackPanel>
<Button>
<Button.Content>
OK
</Button.Content>
<Button.Background>
White
</Button.Background>
</Button>
<Button Content="OK2" Background="White" /> <!-- attribute syntax -->
<Button Content="OK3">
<Button.Background> <!-- property element syntax -->
<SolidColorBrush Color="White" />
</Button.Background>
</Button>
<Button Content="OK4">
<Button.Background>
<SolidColorBrush>
<SolidColorBrush.Color>
<Color A="255" R="255" G="255" B="255"/>
</SolidColorBrush.Color>
</SolidColorBrush>
</Button.Background>
</Button>
<Button Content="OK5">
<Button.Background>
<SolidColorBrush>
<SolidColorBrush.Color>
<Color>
<Color.A>255</Color.A>
<Color.R>255</Color.R>
<Color.G>255</Color.G>
<Color.B>255</Color.B>
</Color>
</SolidColorBrush.Color>
</SolidColorBrush>
</Button.Background>
</Button>
</StackPanel>
</Window>
Here is what the program looks like.

Look at the last button’s XAML. Each 255 string needs to converted into a Byte value expected by the A, R, G, and B properties of the Color type. Type converters enable values to be expressed that couln’t otherwise be expressed. You can write your own type converters.
XAML Introduction Part 2 Syntax
XAML Introduction Part 4 Object Names