Silverlight 5 Hosting :: Simple Sample PivotViewer App in Silverlight 5

I’ve been hoping to share a super simple sample app to showcase PivotViewer in Silverlight 5. I hope this walk through is something you will find useful!

First, let’s take care of the pre-requisites:

  • Install Visual Studio 2010 SP1:

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=23691

  • Notes for the RC of Silverlight 5:

http://www.silverlight.net/learn/overview/what%27s-new-in-silverlight-5

  • Download and install the Silverlight 5 Runtime and Tools for Visual Studio using the links on that page.

Once you get the pre-reqs, create a new Silverlight application. If you haven’t done this much before, here is a handy link:

  • How to create a Silverlight project from scratch (replace 4s for 5s as appropriate):

http://msdn.microsoft.com/en-us/library/cc838164(v=VS.95).aspx

Now, let’s add a PivotViewer. I want to view people by age and birthday. You will need to edit the MainPage.xaml and MainPage.xaml.cs files and add a reference to System.Windows.Controls.Pivot.

MainPage.xaml:

 <UserControl x:Class="Birthdays.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:pv="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <pv:PivotViewer x:Name="pivotviewer" ItemsSource="{Binding}">
            <pv:PivotViewer.PivotProperties>
                <pv:PivotViewerStringProperty Id="Name" Options="CanFilter" DisplayName="Name" Binding="{Binding Name}" />
                <pv:PivotViewerNumericProperty Id="Age" Options="CanFilter" DisplayName="Age" Binding="{Binding Age}" />
                <pv:PivotViewerDateTimeProperty Id="Birthday" Options="CanFilter" DisplayName="Birthday" Binding="{Binding Birthday}" />
            </pv:PivotViewer.PivotProperties>
            <pv:PivotViewer.ItemTemplates>
                <pv:PivotViewerItemTemplate>
                    <Border Width="200" Height="200" Background="DarkSlateBlue">
                        <TextBlock Text="{Binding Name}" FontSize="30" FontFamily="Georgia" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </pv:PivotViewerItemTemplate>
            </pv:PivotViewer.ItemTemplates>
        </pv:PivotViewer>
    </Grid>
</UserControl>

MainPage.xaml.cs:

using System;
using System.Collections.Generic;
using System.Windows.Controls;

namespace Birthdays
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            List<Person> birthdayBook = new List<Person>();
            birthdayBook.Add(new Person("Brad", 25, new DateTime(1986, 5, 9)));
            birthdayBook.Add(new Person("Janet", 12, new DateTime(1998, 12, 3)));
            birthdayBook.Add(new Person("Rocky", 43, new DateTime(1968, 7, 17)));
            birthdayBook.Add(new Person("Magenta", 21, new DateTime(1990, 2, 21)));

            pivotviewer.DataContext = birthdayBook;
        }

        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public DateTime? Birthday { get; set; }

            public Person(string name, int age, DateTime? birthday)
            {
                this.Name = name;
                this.Age = age;
                this.Birthday = birthday;
            }
        }
    }
}

Conclusion
When I compile the code above, I get the sample PivotViewer collection I snapshotted. It should be incredibly easy. You can bind to any data type, you can use whatever object definitions make sense for your data.

Comments are closed.

Sponsors

JaringanHosting.com

Cheap Reseller Hosting

Best Blog Hosting Provider

Best European ASP.NET 4.0 Hosting Provider