Home ยป Silverlight

Silverlight Pager Control

9 Dec 2008 7:24 PM 11 Comments Bookmark and Share kick it on DotNetKicks.com

I like the Digg pager control.  I like it on DotNetKicks too.

Silverlight doesn't have a built in pager-bar control.  So I built one.

This control can be placed within any other Silverlight control to control paging of any sort - not just the plain old DataGrid.

Pager  

 

See a live demo (using a plain old DataGrid).  Download the C# Silverlight code.  See the code below.  Use as you wish.

 

Pager.xaml

   1: <UserControl 
   2:     x:Class="BlogEngine.UI.Pager"
   3:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   4:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   5:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
   6:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
   7:     mc:Ignorable="d" 
   8:     xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
   9:     Width="Auto" Height="Auto">
  10:  
  11:     <UserControl.Resources>
  12:         <Style x:Key="PagerButtonOuterStyle" TargetType="Button">
  13:             <Setter Property="Background" Value="#FF0000FF"/>
  14:             <Setter Property="Foreground" Value="#FF445ECE"/>
  15:             <Setter Property="Padding" Value="3"/>
  16:             <Setter Property="BorderThickness" Value="0.5"/>
  17:             <Setter Property="Template">
  18:                 <Setter.Value>
  19:                     <ControlTemplate TargetType="Button">
  20:                         <Grid Margin="3,3,3,3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
  21:                             <vsm:VisualStateManager.VisualStateGroups>
  22:                                 <vsm:VisualStateGroup x:Name="CommonStates">
  23:                                     <vsm:VisualState x:Name="Normal">
  24:                                         <Storyboard>
  25:                                             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
  26:                                                 <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF0000FF"/>
  27:                                             </ColorAnimationUsingKeyFrames>
  28:                                         </Storyboard>
  29:                                     </vsm:VisualState>
  30:                                     <vsm:VisualState x:Name="MouseOver">
  31:                                         <Storyboard>
  32:                                             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
  33:                                                 <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF99A5D9"/>
  34:                                             </ColorAnimationUsingKeyFrames>
  35:                                         </Storyboard>
  36:                                     </vsm:VisualState>
  37:                                     <vsm:VisualState x:Name="Pressed">
  38:                                         <Storyboard>
  39:                                             <ColorAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
  40:                                                 <SplineColorKeyFrame KeyTime="0" Value="#FF99A5D9"/>
  41:                                             </ColorAnimationUsingKeyFrames>
  42:                                         </Storyboard>
  43:                                     </vsm:VisualState>
  44:                                     <vsm:VisualState x:Name="Disabled">
  45:                                         <Storyboard>
  46:                                             <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity">
  47:                                                 <SplineDoubleKeyFrame KeyTime="0" Value=".55"/>
  48:                                             </DoubleAnimationUsingKeyFrames>
  49:                                             <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(Rectangle.RadiusX)">
  50:                                                 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
  51:                                             </DoubleAnimationUsingKeyFrames>
  52:                                             <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(Rectangle.RadiusY)">
  53:                                                 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
  54:                                             </DoubleAnimationUsingKeyFrames>
  55:                                         </Storyboard>
  56:                                     </vsm:VisualState>
  57:                                 </vsm:VisualStateGroup>
  58:                                 <vsm:VisualStateGroup x:Name="FocusStates">
  59:                                     <vsm:VisualState x:Name="Focused">
  60:                                         <Storyboard>
  61:                                             <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity">
  62:                                                 <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
  63:                                             </DoubleAnimationUsingKeyFrames>
  64:                                         </Storyboard>
  65:                                     </vsm:VisualState>
  66:                                     <vsm:VisualState x:Name="Unfocused"/>
  67:                                 </vsm:VisualStateGroup>
  68:                             </vsm:VisualStateManager.VisualStateGroups>
  69:                             <Border x:Name="Background" Background="#FF0000FF" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="0,0,0,0" BorderThickness="0.5,0.5,0.5,0.5">
  70:                                 <Grid Margin="0,0,1,1" Background="#FFFFFFFF" Width="Auto" HorizontalAlignment="Stretch"/>
  71:                             </Border>
  72:                             <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
  73:                             <Rectangle x:Name="DisabledVisualElement" IsHitTestVisible="false" Opacity="0" Fill="#FFFFFFFF" RadiusX="0" RadiusY="0" StrokeThickness="0.5"/>
  74:                             <Rectangle Margin="1" x:Name="FocusVisualElement" IsHitTestVisible="false" Opacity="0" Stroke="#FF0000FF" StrokeThickness="0.5" RadiusX="0" RadiusY="0"/>
  75:                         </Grid>
  76:                     </ControlTemplate>
  77:                 </Setter.Value>
  78:             </Setter>
  79:         </Style>
  80:         <Style x:Key="PagerButtonInnerStyle" TargetType="Button">
  81:             <Setter Property="Background" Value="#FF0000FF"/>
  82:             <Setter Property="Foreground" Value="#FF445ECE"/>
  83:             <Setter Property="Padding" Value="8,3,8,3"/>
  84:             <Setter Property="BorderThickness" Value="0.5"/>
  85:             <Setter Property="Template">
  86:                 <Setter.Value>
  87:                     <ControlTemplate TargetType="Button">
  88:                         <Grid Margin="3,3,3,3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
  89:                             <vsm:VisualStateManager.VisualStateGroups>
  90:                                 <vsm:VisualStateGroup x:Name="CommonStates">
  91:                                     <vsm:VisualState x:Name="Normal">
  92:                                         <Storyboard>
  93:                                             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
  94:                                                 <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF0000FF"/>
  95:                                             </ColorAnimationUsingKeyFrames>
  96:                                         </Storyboard>
  97:                                     </vsm:VisualState>
  98:                                     <vsm:VisualState x:Name="MouseOver">
  99:                                         <Storyboard>
 100:                                             <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
 101:                                                 <SplineColorKeyFrame KeyTime="00:00:00" Value="#FF99A5D9"/>
 102:                                             </ColorAnimationUsingKeyFrames>
 103:                                         </Storyboard>
 104:                                     </vsm:VisualState>
 105:                                     <vsm:VisualState x:Name="Pressed">
 106:                                         <Storyboard>
 107:                                             <ColorAnimationUsingKeyFrames Storyboard.TargetName="Background" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)">
 108:                                                 <SplineColorKeyFrame KeyTime="0" Value="#FF99A5D9"/>
 109:                                             </ColorAnimationUsingKeyFrames>
 110:                                         </Storyboard>
 111:                                     </vsm:VisualState>
 112:                                     <vsm:VisualState x:Name="Disabled">
 113:                                         <Storyboard>
 114:                                             <DoubleAnimationUsingKeyFrames Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity">
 115:                                                 <SplineDoubleKeyFrame KeyTime="0" Value=".55"/>
 116:                                             </DoubleAnimationUsingKeyFrames>
 117:                                             <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(Rectangle.RadiusX)">
 118:                                                 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
 119:                                             </DoubleAnimationUsingKeyFrames>
 120:                                             <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(Rectangle.RadiusY)">
 121:                                                 <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0"/>
 122:                                             </DoubleAnimationUsingKeyFrames>
 123:                                         </Storyboard>
 124:                                     </vsm:VisualState>
 125:                                 </vsm:VisualStateGroup>
 126:                                 <vsm:VisualStateGroup x:Name="FocusStates">
 127:                                     <vsm:VisualState x:Name="Focused">
 128:                                         <Storyboard>
 129:                                             <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity">
 130:                                                 <SplineDoubleKeyFrame KeyTime="0" Value="1"/>
 131:                                             </DoubleAnimationUsingKeyFrames>
 132:                                         </Storyboard>
 133:                                     </vsm:VisualState>
 134:                                     <vsm:VisualState x:Name="Unfocused"/>
 135:                                 </vsm:VisualStateGroup>
 136:                             </vsm:VisualStateManager.VisualStateGroups>
 137:                             <Border x:Name="Background" Background="#FF0000FF" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="0,0,0,0" BorderThickness="0.5,0.5,0.5,0.5">
 138:                                 <Grid Margin="0,0,1,1" Background="#FFFFFFFF" Width="Auto" HorizontalAlignment="Stretch"/>
 139:                             </Border>
 140:                             <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" x:Name="contentPresenter" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
 141:                             <Rectangle x:Name="DisabledVisualElement" IsHitTestVisible="false" Opacity="0" Fill="#FFFFFFFF" RadiusX="0" RadiusY="0" StrokeThickness="0.5"/>
 142:                             <Rectangle Margin="1" x:Name="FocusVisualElement" IsHitTestVisible="false" Opacity="0" Stroke="#FF0000FF" StrokeThickness="0.5" RadiusX="0" RadiusY="0"/>
 143:                         </Grid>
 144:                     </ControlTemplate>
 145:                 </Setter.Value>
 146:             </Setter>
 147:         </Style>
 148:     </UserControl.Resources>
 149:  
 150:     <Grid x:Name="LayoutRoot" Background="Transparent">
 151:  
 152:         <StackPanel x:Name="spPager" Orientation="Horizontal" Margin="0,10,0,0" />
 153:  
 154:     </Grid>
 155:  
 156: </UserControl>

 

Pager.xaml.cs

   1: using System.Globalization;
   2: using System.Windows;
   3: using System.Windows.Controls;
   4: using System.Windows.Media;
   5:  
   6: namespace BlogEngine.UI
   7: {
   8:     /// <summary>
   9:     /// Renders a Pager control to be used in any fashion
  10:     /// Useful for any control(s) that require paging functionality
  11:     /// </summary>
  12:     public partial class Pager : UserControl
  13:     {
  14:         #region Properties
  15:         /// <summary>
  16:         /// Total Number of Pages for this Pager Control
  17:         /// </summary>
  18:         public int PageCount { get; set; }
  19:         /// <summary>
  20:         /// Current Page Index
  21:         /// </summary>
  22:         public int PageIndex { get; set; }
  23:         /// <summary>
  24:         /// Number of buttons to render before/after current selection
  25:         /// For example, if = 2, buttons appear for a control with 100 pages
  26:         /// /// Button 5 selected:
  27:         ///     Previous 1 2 3 4 (5) 6 7 ... 99 100 Next
  28:         /// Button 6 selected:
  29:         ///     Previous 1 2 ... 4 5 (6) 7 8 ... 99 100 Next
  30:         /// Button 95 selected
  31:         ///     Previous 1 2 ... 93 94 (95) 96 97 ... 99 100 Next
  32:         /// Button 96 selected
  33:         ///     Previous 1 2 ... 94 95 (96) 97 98 99 100 Next
  34:         /// </summary>
  35:         public int PageButtonCount { get; set; }
  36:         /// <summary>
  37:         /// EVent delegate for a specific Pager Button
  38:         /// </summary>
  39:         /// <param name="sender"></param>
  40:         /// <param name="e"></param>
  41:         public delegate void PagerButtonClick(object sender, RoutedEventArgs e);
  42:         /// <summary>
  43:         /// Event handler for a specific Pager Button
  44:         /// </summary>
  45:         public event PagerButtonClick Click;
  46:         #endregion
  47:  
  48:         public Pager()
  49:         {
  50:             InitializeComponent();
  51:  
  52:             // Default Settings
  53:             PageCount = 15;
  54:             PageIndex = 1;
  55:             PageButtonCount = 3;
  56:  
  57:             this.Loaded += new RoutedEventHandler(Pager_Loaded);
  58:         }
  59:         public Pager(int pageCount, int pageButtonCount)
  60:         {
  61:             InitializeComponent();
  62:  
  63:             PageCount = pageCount;
  64:             PageIndex = 1;
  65:             PageButtonCount = pageButtonCount;
  66:  
  67:             this.Loaded += new RoutedEventHandler(Pager_Loaded);
  68:         }
  69:  
  70:         protected void Pager_Loaded(object sender, RoutedEventArgs e)
  71:         {
  72:             BuildPager();
  73:         }
  74:  
  75:         #region Internal
  76:         /// <summary>
  77:         /// Renders a Button control for the Pager
  78:         /// </summary>
  79:         /// <param name="text">Text to display</param>
  80:         /// <param name="inner">Flag to denote if this refers to the inner buttons or the Previous/Next buttons</param>
  81:         /// <param name="enabled">Flag to denote if this button is enabled</param>
  82:         /// <returns>Button</returns>
  83:         private Button BuildButton(string text, bool inner, bool enabled)
  84:         {
  85:             Button b = new Button()
  86:             {
  87:                 Content = text,
  88:                 Tag = text,
  89:                 Style = inner ? this.Resources["PagerButtonInnerStyle"] as Style : this.Resources["PagerButtonOuterStyle"] as Style,
  90:                 Width = 45
  91:             };
  92:  
  93:             if (inner == false)
  94:                 b.Width = 75;
  95:  
  96:             b.IsEnabled = enabled;
  97:  
  98:             b.Click += new RoutedEventHandler(PagerButton_Click);
  99:  
 100:             return b;
 101:         }
 102:         /// <summary>
 103:         /// Renders a selected Button or the Hellip (...) TextBlock
 104:         /// </summary>
 105:         /// <param name="text">Text to display</param>
 106:         /// <param name="border">Flag to denote if this button is to have a border</param>
 107:         /// <returns>UIElement (either a TextBlock or a Border with a TextBlock within)</returns>
 108:         private UIElement BuildSpan(string text, bool border)
 109:         {
 110:             if (border)
 111:             {
 112:                 TextBlock t = new TextBlock()
 113:                 {
 114:                     Text = text,
 115:                     TextAlignment = TextAlignment.Center,
 116:                     VerticalAlignment = VerticalAlignment.Center,
 117:                     Width = 30
 118:                 };
 119:                 Border b = new Border()
 120:                 {
 121:                     Margin = new Thickness(3, 3, 3, 3),
 122:                     BorderThickness = new Thickness(0.5, 0.5, 0.5, 0.5),
 123:                     BorderBrush = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255)),
 124:                     Width = 30,
 125:                     Height = 22,
 126:                     HorizontalAlignment = HorizontalAlignment.Center,
 127:                     VerticalAlignment = VerticalAlignment.Center
 128:                 };
 129:                 b.Child = t;
 130:                 return b;
 131:             }
 132:             else
 133:             {
 134:                 return new TextBlock()
 135:                 {
 136:                     Text = text,
 137:                     Width = 30,
 138:                     Height = 22,
 139:                     TextAlignment = TextAlignment.Center,
 140:                     VerticalAlignment = VerticalAlignment.Bottom
 141:                 };
 142:             }
 143:         }
 144:         
 145:         /// <summary>
 146:         /// Build the Pager Control
 147:         /// </summary>
 148:         private void BuildPager()
 149:         {
 150:             this.spPager.Children.Clear();
 151:  
 152:             if (PageCount > 1)
 153:             {
 154:                 int min = PageIndex - PageButtonCount;
 155:                 int max = PageIndex + PageButtonCount;
 156:  
 157:                 if (max > PageCount)
 158:                     min -= max - PageCount;
 159:                 else if (min < 1)
 160:                     max += 1 - min;
 161:  
 162:                 // Previous Button
 163:                 if (PageIndex > 1)
 164:                     this.spPager.Children.Add(BuildButton("Previous", false, true));
 165:                 else // Disabled State
 166:                     this.spPager.Children.Add(BuildButton("Previous", false, false));
 167:  
 168:                 // Middle Buttons
 169:                 bool needDiv = false;
 170:                 for (int i = 1; i <= PageCount; i++)
 171:                 {
 172:                     if (i <= 2 || i > PageCount - 2 || (min <= i && i <= max))
 173:                     {
 174:                         string text = i.ToString(NumberFormatInfo.InvariantInfo);
 175:  
 176:                         if (i == PageIndex) // Currently Selected Index
 177:                             this.spPager.Children.Add(BuildSpan(text, false));
 178:                         else
 179:                             this.spPager.Children.Add(BuildButton(text, true, true));
 180:  
 181:                         needDiv = true;
 182:                     }
 183:                     else if (needDiv)
 184:                     {
 185:                         // This will add the hellip (...) TextBlock
 186:                         this.spPager.Children.Add(BuildSpan("...", false));
 187:                         needDiv = false;
 188:                     }
 189:                 }
 190:  
 191:                 // Next Button
 192:                 if (PageIndex < PageCount)
 193:                     this.spPager.Children.Add(BuildButton("Next", false, true));
 194:                 else // Disabled State
 195:                     this.spPager.Children.Add(BuildButton("Next", false, false));
 196:             }
 197:         }
 198:         #endregion
 199:  
 200:         #region Event Handlers
 201:         /// <summary>
 202:         /// Handles Pager Button click
 203:         /// Sets proper PageIndex for rendering
 204:         /// </summary>
 205:         /// <param name="sender"></param>
 206:         /// <param name="e"></param>
 207:         protected void PagerButton_Click(object sender, RoutedEventArgs e)
 208:         {
 209:             Button b = e.OriginalSource as Button;
 210:  
 211:             if (b.Tag.ToString() == "Previous")
 212:             {
 213:                 PageIndex--;
 214:             }
 215:             else if (b.Tag.ToString() == "Next")
 216:             {
 217:                 PageIndex++;
 218:             }
 219:             else
 220:             {
 221:                 int p = PageIndex;
 222:                 int.TryParse(b.Tag.ToString(), out p);
 223:                 PageIndex = p;
 224:             }
 225:             
 226:             BuildPager();
 227:             Click(sender, e);
 228:         }
 229:         #endregion
 230:     }
 231: }

 

PagerDemo.xaml

   1: <UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"  x:Class="BlogEngine.UI.PagerDemo"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
   4:     xmlns:Pager="clr-namespace:BlogEngine.UI"
   5:     Width="Auto" Height="Auto">
   6:     
   7:     <Grid x:Name="LayoutRoot" Background="BlanchedAlmond">
   8:  
   9:         <StackPanel Orientation="Vertical" Margin="5,5,5,5" >
  10:  
  11:             <data:DataGrid x:Name="dgItems" 
  12:                                AutoGenerateColumns="False" 
  13:                                IsReadOnly="True" Width="Auto">
  14:                 <data:DataGrid.Columns>
  15:                     <data:DataGridTextColumn Header="First Name" Binding="{Binding FirstName}" SortMemberPath="FirstName" />
  16:                     <data:DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" SortMemberPath="LastName" />
  17:                     <data:DataGridTextColumn Header="Phone" Binding="{Binding PhoneNumber}" SortMemberPath="PhoneNumber" />
  18:                 </data:DataGrid.Columns>
  19:             </data:DataGrid>
  20:             
  21:             <StackPanel x:Name="spPager" HorizontalAlignment="Center" />
  22:         
  23:         </StackPanel>
  24:  
  25:     </Grid>
  26:  
  27: </UserControl>

 

PagerDemo.xaml.cs

   1: using System.Collections.Generic;
   2: using System.Linq;
   3: using System.Windows;
   4: using System.Windows.Controls;
   5:  
   6: namespace BlogEngine.UI
   7: {
   8:     public partial class PagerDemo : UserControl
   9:     {
  10:         #region Variables
  11:         private List<SampleObject> _items = new List<SampleObject>();
  12:         private int _pageSize = 5;
  13:         private int _skip = 0;
  14:         #endregion
  15:  
  16:         public PagerDemo()
  17:         {
  18:             InitializeComponent();
  19:             this.Loaded += new RoutedEventHandler(PagerDemo_Loaded);
  20:         }
  21:  
  22:         protected void PagerDemo_Loaded(object sender, RoutedEventArgs e)
  23:         {
  24:             _items = BuildData();
  25:  
  26:             Pager pager = new Pager(_items.Count / _pageSize, 2);
  27:             pager.Click += new Pager.PagerButtonClick(PagerControl_Click);
  28:  
  29:             if (_skip == 0)
  30:                 pager.PageIndex = 1;
  31:             else
  32:                 pager.PageIndex = (_skip / _pageSize) + 1;
  33:  
  34:             dgItems.ItemsSource = _items.Skip(_skip).Take(_pageSize);
  35:             
  36:             spPager.Children.Clear();
  37:             spPager.Children.Add(pager);
  38:         }
  39:  
  40:         #region Internal
  41:         private List<SampleObject> BuildData()
  42:         {
  43:             List<SampleObject> items = new List<SampleObject>();
  44:             
  45:             for (int i = 1; i < 101; i++)
  46:             {
  47:                 items.Add(new SampleObject { FirstName = "Steve" + i.ToString(), LastName = "Dunlap", PhoneNumber = "123.456.7890" });
  48:                 items.Add(new SampleObject { FirstName = "Tara" + i.ToString(), LastName = "Dunlap", PhoneNumber = "123.456.7890" });
  49:                 items.Add(new SampleObject { FirstName = "Kevin" + i.ToString(), LastName = "Jones", PhoneNumber = "123.456.7890" });
  50:                 items.Add(new SampleObject { FirstName = "Jane" + i.ToString(), LastName = "Smith", PhoneNumber = "123.456.7890" });
  51:                 items.Add(new SampleObject { FirstName = "John" + i.ToString(), LastName = "Smith", PhoneNumber = "123.456.7890" });
  52:             }
  53:  
  54:             return items;
  55:         }
  56:         #endregion
  57:  
  58:         #region Event Handler
  59:         protected void PagerControl_Click(object sender, RoutedEventArgs e)
  60:         {
  61:             Button b = e.OriginalSource as Button;
  62:  
  63:             if (b.Content.ToString() == "Previous")
  64:                 _skip -= _pageSize;
  65:             else if (b.Content.ToString() == "Next")
  66:                 _skip += _pageSize;
  67:             else
  68:             {
  69:                 int.TryParse(b.Content.ToString(), out _skip);
  70:                 _skip = (_skip * _pageSize) - _pageSize;
  71:             }
  72:  
  73:             if (_skip < 0)
  74:                 _skip = 0;
  75:  
  76:             dgItems.ItemsSource = _items.Skip(_skip).Take(_pageSize);
  77:         }
  78:         #endregion
  79:     }
  80:     public class SampleObject
  81:     {
  82:         public string FirstName { get; set; }
  83:         public string LastName { get; set; }
  84:         public string PhoneNumber { get; set; }
  85:     }
  86: }

Currently rated 3.0 by 5 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Comments are closed