15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

o v e r v i e w ❘ 731<br />

A type converter derives from the base class TypeConverter in the System.<br />

ComponentModel namespace. The type of the class that needs conversion defi nes the<br />

type converter with the TypeConverter attribute. WPF uses many type converters to<br />

convert XML attributes to a specifi c type. ColorConverter , FontFamilyConverter ,<br />

PathFigureCollectionConverter , ThicknessConverter , GeometryConverter are<br />

just a few of a large number of type converters.<br />

Properties as elements<br />

It ’ s always also possible to use the element syntax to supply the value for properties. The Background<br />

property of the Button class can be set with the child element Button.Background . This way more<br />

complex brushes can be applied to this property, for example, a LinearGradientBrush , as shown in<br />

the example.<br />

When setting the content in the sample, neither the Content attribute nor a Button.Content element is<br />

used to write the content; instead, the content is written directly as a child value to the Button element.<br />

That ’ s possible because with a base class of the Button class ( ContentControl ), the ContentProperty<br />

attribute is applied, which marks the Content property as a ContentProperty : [ContentProperty<br />

( “ Content “ )] . With such a marked property, the value of the property can be written as child element.<br />

< Button ><br />

Click Me!<br />

< Button.Background ><br />

< LinearGradientBrush StartPoint="0.5,0.0" EndPoint="0.5, 1.0" ><br />

< GradientStop Offset="0" Color="Yellow" / ><br />

< GradientStop Offset="0.3" Color="Orange" / ><br />

< GradientStop Offset="0.7" Color="Red" / ><br />

< GradientStop Offset="1" Color="DarkRed" / ><br />

< /LinearGradientBrush ><br />

< /Button.Background ><br />

< /Button ><br />

code snippet XAMLSyntax/MainWindow.xaml<br />

essential .neT Types<br />

In XAML 2006, core .<strong>NET</strong> types need to be referenced from an XML namespace like all other .<strong>NET</strong><br />

classes, for example, the String with the sys alias, as shown here:<br />

< sys:String xmlns:sys="clr-namespace:System;assembly=mscorlib > Simple String < /sys:String ><br />

XAML 2009 defi nes types such as String , Boolean , Object , Decimal , Double , Int32 <strong>and</strong> others with the<br />

x alias.<br />

< x:String > Simple String < /x:String ><br />

Collections<br />

In the ListBox that contains Person elements, you ’ ve already seen a collection within XAML. In the<br />

ListBox , the items have been directly defi ned as child elements. Also, the LinearGradientBrush contained<br />

a collection of GradientStop elements. This is possible because the base class ItemsControl has the<br />

attribute ContentProperty set to the Items property of the class, <strong>and</strong> the GradientBrush base class sets<br />

the attribute ContentProperty to GradientStops .<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!