728x90
반응형
728x170
▶ PagingCollectionView.cs
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
}
}
728x90
▶ MainWindow.xaml
<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
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
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 스플래시 화면 이미지 사용하기 (0) | 2022.01.12 |
---|---|
[C#/WPF] Application 클래스 : Startup/Exit 이벤트를 사용해 애플리케이션 시작/종료시 처리하기 (0) | 2022.01.11 |
[C#/WPF] Application 클래스 : Activated/Deactivated 이벤트를 사용해 애플리케이션 활성화/비활성화시 처리하기 (0) | 2022.01.11 |
[C#/WPF] Application 클래스 : Startup 이벤트를 사용해 애플리케이션 시작시 처리하기 (0) | 2022.01.11 |
[C#/WPF] Application 클래스 : Current 정적 속성을 사용해 현재 애플리케이션 객체 구하기 (0) | 2022.01.11 |
[C#/WPF] Path 엘리먼트 : 루프 아이콘 만들기 (0) | 2022.01.10 |
[C#/WPF] ControlTemplate 엘리먼트 : 볼륨 미터 ProgressBar 엘리먼트 정의하기 (0) | 2022.01.10 |
[C#/WPF] ControlTemplate 엘리먼트 : 원형 Button 엘리먼트 정의하기 (0) | 2022.01.10 |
[C#/WPF] Path 엘리먼트 : 별점(Star Rating) 아이콘 만들기 (0) | 2022.01.09 |
[C#/WPF] Path 엘리먼트 : RSS 아이콘 만들기 (0) | 2022.01.09 |
댓글을 달아 주세요