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.

1068 ❘ ChaPTer 36 business ApplicAtiOns with wpf<br />

through pages. In a simple scenario, only the ItemsSource property of the DataGrid needs to be assigned.<br />

By default, the DataGrid creates columns based on the properties of the bound data:<br />

<br />

<br />

<br />

Previous<br />

Next<br />

<br />

<br />

Previous<br />

Next<br />

<br />

<br />

<br />

<br />

code snippet Formula1Demo/GridUC.xaml<br />

The code-behind uses the same Formula1 database as the previous TreeView example. The DataContext of<br />

the UserControl is set to the Races property. This property returns IEnumerable.<br />

Instead of assigning a strongly typed enumeration, an object is used to make it possible to create an<br />

anonymous class with the LINQ query. The LINQ query creates the anonymous class with Year, Country,<br />

Position, Racer, <strong>and</strong> Car properties <strong>and</strong> uses a compound to access Races <strong>and</strong> RaceResults. It also<br />

accesses other associations of Races to get country, racer, <strong>and</strong> team information. With the Skip() <strong>and</strong><br />

Take() methods, paging functionality is implemented. The size of a page is fixed to 50 items, <strong>and</strong> the<br />

current page changes with the OnNext() <strong>and</strong> OnPrevious() h<strong>and</strong>lers:<br />

using System.Collections.Generic;<br />

using System.Linq;<br />

using System.Windows;<br />

using System.Windows.Controls;<br />

namespace Formula1Demo<br />

{<br />

public partial class GridUC : UserControl<br />

{<br />

private int currentPage = 0;<br />

private int pageSize = 50;<br />

private Formula1Entities data = new Formula1Entities();<br />

public GridUC()<br />

{<br />

InitializeComponent();<br />

this.DataContext = Races;<br />

}<br />

public IEnumerable Races<br />

{<br />

get<br />

{<br />

return (from r in data.Races<br />

from rr in r.RaceResults<br />

orderby r.Date ascending<br />

select new<br />

{<br />

Year = r.Date.Year,<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!