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.

1012 ❘ ChaPTer 35 cOre wpf<br />

In WPF, the functionality of controls is completely separate from their look <strong>and</strong> feel. A button has a default<br />

look, but you can completely customize that look as you like with templates.<br />

WPF gives you several template types that derive from the base class FrameworkTemplate.<br />

TemPlaTe TyPe<br />

ControlTemplate<br />

ItemsPanelTemplate<br />

DataTemplate<br />

HierarchicalDataTemplate<br />

desCriPTion<br />

With a ControlTemplate you can specify the visual structure of a control<br />

<strong>and</strong> override the look.<br />

For an ItemsControl you can specify the layout of its items by<br />

assigning an ItemsPanelTemplate. Each ItemsControl has a<br />

default ItemsPanelTemplate. For the MenuItem, it is a WrapPanel.<br />

The StatusBar uses a DockPanel, <strong>and</strong> the ListBox uses a<br />

VirtualizingStackPanel.<br />

DataTemplates are very useful for graphical representations of objects.<br />

Styling a ListBox, you will see that, by default, the items of the ListBox<br />

are shown according to the output of the ToString() method. By applying<br />

a DataTemplate you can override this behavior <strong>and</strong> define a custom<br />

presentation of the items.<br />

The HierarchicalDataTemplate is used for arranging a tree of objects.<br />

This control supports HeaderedItemsControls, such as TreeViewItem<br />

<strong>and</strong> MenuItem.<br />

Control Templates<br />

Previously in this chapter you’ve seen how the properties of a control can be styled. If setting simple<br />

properties of the controls doesn’t give you the look you want, you can change the Template property. With<br />

the Template property, the complete look of the control can be customized.<br />

The next example shows customizing buttons, <strong>and</strong> later, list boxes are customized step by step, so you can<br />

see the intermediate results of the changes.<br />

The customization of the Button type is done in a separate resource dictionary file, Styles.xaml. Here, a<br />

style with the key name RoundedGelButton is defined.<br />

The style GelButton sets the properties Background, Height, Foreground, <strong>and</strong> Margin, <strong>and</strong> the<br />

Template. The Template is the most interesting aspect with this style. The Template specifies a Grid<br />

with just one row <strong>and</strong> one column.<br />

Inside this cell, you can find an ellipse with the name GelBackground. This ellipse has a linear gradient brush<br />

for the stroke. The stroke that surrounds the rectangle is very thin because the StrokeThickness is set to 0.5.<br />

The second ellipse, GelShine, is a small ellipse where the size is defined by the Margin property <strong>and</strong><br />

so is visible within the first ellipse. The stroke is transparent, so there is no line surrounding the ellipse.<br />

This ellipse uses a linear gradient fill brush, which goes from a light, partly transparent color to full<br />

transparency. This gives the ellipse a shimmering effect:<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!