■ CollectionView 클래스를 사용해 DataGrid 객체에서 페이징을 처리하는 방법을 보여준다.
▶ PagingCollectionView.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
using System.Collections; using System.ComponentModel; using System.Windows.Data; namespace TestProject { /// <summary> /// 페이징 컬렉션 뷰 /// </summary> public class PagingCollectionView : CollectionView { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 소스 리스트 /// </summary> private readonly IList sourceList; /// <summary> /// 페이지당 항목 수 /// </summary> private readonly int itemCountPerPage; /// <summary> /// 현재 페이지 /// </summary> private int currentPage = 1; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 페이지당 항목 수 - ItemCountPerPage /// <summary> /// 페이지당 항목 수 /// </summary> public int ItemCountPerPage { get { return this.itemCountPerPage; } } #endregion #region 페이지 수 - PageCount /// <summary> /// 페이지 수 /// </summary> public int PageCount { get { return (this.sourceList.Count + this.itemCountPerPage - 1) / this.itemCountPerPage; } } #endregion #region 현재 페이지 - CurrentPage /// <summary> /// 현재 페이지 /// </summary> public int CurrentPage { get { return this.currentPage; } set { this.currentPage = value; OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage")); } } #endregion #region 카운트 - Count /// <summary> /// 카운트 /// </summary> public override int Count { get { if(this.sourceList.Count == 0) { return 0; } if(this.currentPage < PageCount) { return this.itemCountPerPage; } else { int itemCountLeft = this.sourceList.Count % this.itemCountPerPage; if(itemCountLeft == 0) { return this.itemCountPerPage; } else { return itemCountLeft; } } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 시작 인덱스 - StartIndex /// <summary> /// 시작 인덱스 /// </summary> private int StartIndex { get { return (this.currentPage - 1) * this.itemCountPerPage; } } #endregion #region 종료 인덱스 - EndIndex /// <summary> /// 종료 인덱스 /// </summary> private int EndIndex { get { int index = this.currentPage * this.itemCountPerPage - 1; return (index > this.sourceList.Count) ? this.sourceList.Count : index; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PagingCollectionView(sourceList, itemCountPerPage) /// <summary> /// 생성자 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <param name="itemCountPerPage">페이지당 항목 수</param> public PagingCollectionView(IList sourceList, int itemCountPerPage) : base(sourceList) { this.sourceList = sourceList; this.itemCountPerPage = itemCountPerPage; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 구하기 - GetItemAt(index) /// <summary> /// 항목 구하기 /// </summary> /// <param name="index">인덱스</param> /// <returns>항목</returns> public override object GetItemAt(int index) { int offset = index % this.itemCountPerPage; return this.sourceList[StartIndex + offset]; } #endregion #region 이전 페이지로 이동하기 - MoveToPreviousPage() /// <summary> /// 이전 페이지로 이동하기 /// </summary> public void MoveToPreviousPage() { if(this.currentPage > 1) { CurrentPage -= 1; } Refresh(); } #endregion #region 다음 페이지로 이동하기 - MoveToNextPage() /// <summary> /// 다음 페이지로 이동하기 /// </summary> public void MoveToNextPage() { if(this.currentPage < PageCount) { CurrentPage += 1; } Refresh(); } #endregion } } |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="CollectionView 클래스 : DataGrid 객체에서 페이징 처리하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <StackPanel Grid.Row="0" Orientation="Horizontal"> <TextBlock VerticalAlignment="Center" Text="{Binding Path=CurrentPage, StringFormat=현재 페이지 : {0}}" /> <Button Name="previousPageButton" Margin="10 0 0 0" Width="100" Height="30" Content="이전 페이지" /> <Button Name="nextPageButton" Margin="10 0 0 0" Width="100" Height="30" Content="다음 페이지" /> </StackPanel> <DataGrid Grid.Row="1" Margin="0 10 0 0" ItemsSource="{Binding}"> <DataGrid.Columns> <DataGridTextColumn Width="100" Header="동물" Binding="{Binding Animal}" /> <DataGridTextColumn Width="300" Header="먹이" Binding="{Binding Eats}" /> </DataGrid.Columns> </DataGrid> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
using System.Collections.Generic; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 페이징 컬렉션 뷰 /// </summary> private readonly PagingCollectionView collectionView; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.collectionView = new PagingCollectionView ( new List<object> { new { Animal = "Lion" , Eats = "Tiger" }, new { Animal = "Tiger" , Eats = "Bear" }, new { Animal = "Bear" , Eats = "Oh my" }, new { Animal = "Wait" , Eats = "Oh my isn't an animal" }, new { Animal = "Oh well" , Eats = "Who is counting anyway" }, new { Animal = "Need better content", Eats = "For posting on stackoverflow" } }, 2 ); DataContext = this.collectionView; this.previousPageButton.Click += previousPageButton_Click; this.nextPageButton.Click += nextPageButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 이전 페이지 버튼 클릭시 처리하기 - previousPageButton_Click(sender, e) /// <summary> /// 이전 페이지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void previousPageButton_Click(object sender, RoutedEventArgs e) { this.collectionView.MoveToPreviousPage(); } #endregion #region 다음 페이지 버튼 클릭시 처리하기 - nextPageButton_Click(sender, e) /// <summary> /// 다음 페이지 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void nextPageButton_Click(object sender, RoutedEventArgs e) { this.collectionView.MoveToNextPage(); } #endregion } } |