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.

1016 ❘ ChaPTer 35 cOre wpf<br />

With the XAML code, a simple Button element with the name button1 is defined:<br />

<br />

Within the code-behind, a new Country object is instantiated that is assigned to the Content property of<br />

button1:<br />

public StyledButtonWindow()<br />

{<br />

InitializeComponent();<br />

button1.Content = new Country<br />

{<br />

Name = "Austria",<br />

ImagePath = "images/Austria.bmp"<br />

};<br />

}<br />

After running the application, you can see that the DataTemplate is applied<br />

to the Button because Country data type has a default template, as shown<br />

in Figure 35-24.<br />

Of course it is also possible to create a control template <strong>and</strong> use a data<br />

template from within.<br />

styling a listbox<br />

code snippet TemplateDemo/StyledButtonWindow.xaml.cs<br />

figure 35-24<br />

Changing a style of a button or a label is a simple task. How about changing the style of an element that<br />

contains a list of elements For example, how about changing a ListBox Again, a list box has behavior<br />

<strong>and</strong> a look. It can display a list of elements, <strong>and</strong> you can select one or more elements from the list. For the<br />

behavior, the ListBox class defines methods, properties, <strong>and</strong> events. The look of the ListBox is separate<br />

from its behavior. The ListBox element has a default look, but you can change this look by creating a<br />

template.<br />

With a ListBox the ControlTemplate defines how the complete control looks, an ItemTemplate defines<br />

how an item looks, <strong>and</strong> a DataTemplate defines the type that might be within an item.<br />

For filling a ListBox with some items, the static class Countries returns a list of a few countries that will<br />

be displayed:<br />

public class Countries<br />

{<br />

public static IEnumerable GetCountries()<br />

{<br />

return new List<br />

{<br />

new Country { Name = "Austria", ImagePath = "Images/Austria.bmp" },<br />

new Country { Name = "Germany", ImagePath = "Images/Germany.bmp" },<br />

new Country { Name = "Norway", ImagePath = "Images/Norway.bmp" },<br />

new Country { Name = "USA", ImagePath = "Images/USA.bmp" }<br />

};<br />

}<br />

}<br />

code snippet TemplateDemo/Countries.cs<br />

Inside the code-behind file in the constructor of the StyledListBoxWindow1 class, the DataContext<br />

property of the StyledListBoxWindow1 instance is set to the list of countries that is returned from the<br />

method Countries.GetCountries(). (The DataContext property is a data binding feature discussed in<br />

the next chapter.)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!