15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

DataGrid ❘ 1069<br />

}<br />

}<br />

Country = r.Circuit.Country,<br />

Position = rr.Position,<br />

Racer = rr.Racer.Firstname + " " + rr.Racer.Lastname,<br />

Car = rr.Team.Name<br />

}).Skip(currentPage * pageSize).Take(pageSize);<br />

}<br />

}<br />

private void OnPrevious(object sender, RoutedEventArgs e)<br />

{<br />

if (currentPage > 0)<br />

{<br />

currentPage--;<br />

this.DataContext = Races;<br />

}<br />

}<br />

private void OnNext(object sender, RoutedEventArgs e)<br />

{<br />

currentPage++;<br />

this.DataContext = Races;<br />

}<br />

code snippet Formula1Demo/GridUC.xaml.cs<br />

Figure 36-15 shows the running application<br />

with the default grid styles <strong>and</strong> headers.<br />

In the next DataGrid example, the grid is<br />

customized with custom columns <strong>and</strong> grouping.<br />

Custom Columns<br />

Setting the property AutoGenerateColumns<br />

of the DataGrid to False doesn’t generate<br />

default columns. You can create custom<br />

columns with the Columns property. You<br />

can also specify elements that derive from<br />

DataGridColumn. Predefined classes exist<br />

that can be used. DataGridTextColumn<br />

can be used to read <strong>and</strong> edit text.<br />

DataGridHyperlinkColumn is for displaying<br />

hyperlinks. DataGridCheckBoxColumn<br />

figure 36-15<br />

displays a check box for Boolean data. For<br />

a list of items in a column you can use the<br />

DataGridComboBoxColumn. More DataGridColumn types will be available in the future, but if you need a<br />

different representation now, you can use the DataGridTemplateColumn to define <strong>and</strong> bind any elements<br />

you want.<br />

The example code uses DataGridTextColumn elements that are bound to the Position <strong>and</strong> Racer<br />

properties. The Header property is set to a string for display. Of course you can also use a template to define<br />

a complete custom header for the column:<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!