첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
0.01MB

▶ 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 전체 항목 수 - 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(0 == itemCountLeft)
                    {
                        return this.itemCountPerPage;
                    }
                    else
                    {
                        return itemCountLeft;
                    }
                }
            }
        }

        #endregion
        #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

        ////////////////////////////////////////////////////////////////////////////////////////// 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 endIndex = this.currentPage * this.itemCountPerPage - 1;

                return (endIndex > this.sourceList.Count) ? this.sourceList.Count : endIndex;
            }
        }

        #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>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"    />
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0"
            Orientation="Horizontal">
            <Button
                Margin="10 10 10 10"
                Width="120"
                Height="30"
                Content="이전 페이지"
                Click="previousButton_Click" />
            <Button
                Margin="10 10 10 10"
                Width="120"
                Height="30"
                Content="다음 페이지"
                Click="nextButton_Click" />
            <Label Margin="10 10 10 10">
                <Label.Content>
                    <Binding Path="CurrentPage">
                        <Binding.StringFormat>현재 페이지 : {0}</Binding.StringFormat>
                    </Binding>
                </Label.Content>
            </Label>
        </StackPanel>
        <DataGrid Grid.Row="1"
            ItemsSource="{Binding}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="동물" Width="Auto" Binding="{Binding Animal}" />
                <DataGridTextColumn Header="먹이" Width="Auto" Binding="{Binding Eats}"   />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

 

300x250

 

▶ 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;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 이전 페이지 버튼 클릭시 처리하기 - previousButton_Click(sender, e)

        /// <summary>
        /// 이전 페이지 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            this.collectionView.MoveToPreviousPage();
        }

        #endregion
        #region 다음 페이지 버튼 클릭시 처리하기 - nextButton_Click(sender, e)

        /// <summary>
        /// 다음 페이지 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            this.collectionView.MoveToNextPage();
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요