WPF DataGrid에 페이지 매기는?
wpf에서 페이징을 설정하려면 어떻게 해야 합니까?DataGrid
?
위의 코드프로젝트 기사는 ADO 테이블로 이 작업을 수행하는 데 매우 유용합니다.대부분의 어플리케이션에서는 매우 효과적이고 이해하기 쉽지만 Collection Views를 사용하는 'WPF-zen-like' 방법도 있습니다.위의 예와 비교하여 Collection View를 사용하는 장점은 그리드에 삽입하는 데이터가 좀 더 일반적이라는 것입니다(이 예를 더 일반화할 수 없다는 것은 아닙니다).또한 일반적인 WPF 데이터 바인딩 모델에 적합합니다.필요한 경우 정렬, 그룹화 등의 일반적인 작업을 지원할 수 있습니다.
에 바인드되어 있는 Paging Collection View의 극히 간단한 예를 정리했습니다.NET 4.0 DataGrid 제어.이 예 자체는 매우 간단하지만, MoveToNextPage 및 MoveToPreviousPage와 같은 간단한 작업을 실행할 수 있는 실제 데이터 수집 주위에 프록시가 있기 때문에 시작하는 방법은 최소한 보여 줍니다.
다음은 Window 이벤트 처리와 Paging Collection View 모두에 대한 C#입니다.
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
namespace GridPagingExample
{
public partial class MainWindow : Window
{
private readonly PagingCollectionView _cview;
public MainWindow()
{
InitializeComponent();
this._cview = 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
);
this.DataContext = this._cview;
}
private void OnNextClicked(object sender, RoutedEventArgs e)
{
this._cview.MoveToNextPage();
}
private void OnPreviousClicked(object sender, RoutedEventArgs e)
{
this._cview.MoveToPreviousPage();
}
}
public class PagingCollectionView : CollectionView
{
private readonly IList _innerList;
private readonly int _itemsPerPage;
private int _currentPage = 1;
public PagingCollectionView(IList innerList, int itemsPerPage)
: base(innerList)
{
this._innerList = innerList;
this._itemsPerPage = itemsPerPage;
}
public override int Count
{
get
{
if (this._innerList.Count == 0) return 0;
if (this._currentPage < this.PageCount) // page 1..n-1
{
return this._itemsPerPage;
}
else // page n
{
var itemsLeft = this._innerList.Count % this._itemsPerPage;
if (0 == itemsLeft)
{
return this._itemsPerPage; // exactly itemsPerPage left
}
else
{
// return the remaining items
return itemsLeft;
}
}
}
}
public int CurrentPage
{
get { return this._currentPage; }
set
{
this._currentPage = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage"));
}
}
public int ItemsPerPage { get { return this._itemsPerPage; } }
public int PageCount
{
get
{
return (this._innerList.Count + this._itemsPerPage - 1)
/ this._itemsPerPage;
}
}
private int EndIndex
{
get
{
var end = this._currentPage * this._itemsPerPage - 1;
return (end > this._innerList.Count) ? this._innerList.Count : end;
}
}
private int StartIndex
{
get { return (this._currentPage - 1) * this._itemsPerPage; }
}
public override object GetItemAt(int index)
{
var offset = index % (this._itemsPerPage);
return this._innerList[this.StartIndex + offset];
}
public void MoveToNextPage()
{
if (this._currentPage < this.PageCount)
{
this.CurrentPage += 1;
}
this.Refresh();
}
public void MoveToPreviousPage()
{
if (this._currentPage > 1)
{
this.CurrentPage -= 1;
}
this.Refresh();
}
}
}
윈도우의 XAML은 다음과 같습니다.
<Window x:Class="GridPagingExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<Label Grid.Row="0" Margin="2">
<Label.Content>
<Binding Path="CurrentPage">
<Binding.StringFormat>Current Page: {0}</Binding.StringFormat>
</Binding>
</Label.Content>
</Label>
<Button Content="Next" Click="OnNextClicked" Margin="2"/>
<Button Content="Previous" Click="OnPreviousClicked" Margin="2"/>
</StackPanel>
<DataGrid ItemsSource="{Binding}" Grid.Row="1">
<DataGrid.Columns>
<DataGridTextColumn Header="Animal" Width="*" Binding="{Binding Animal}"/>
<DataGridTextColumn Header="Eats" Width="*" Binding="{Binding Eats}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
MoveToLastPage 및 MoveToFirstPage와 같은 사소한 기능이나 정렬과 같이 원하는 동작 방법을 좀 더 고려해야 하는 기능을 지원하기 위해 이 CollectionView를 구축할 수 있습니다.도움이 되길 바랍니다.
언급URL : https://stackoverflow.com/questions/784726/how-can-i-paginate-a-wpf-datagrid
'programing' 카테고리의 다른 글
디렉토리와 하위 디렉토리에서 가장 큰 파일을 찾으려면 어떻게 해야 합니까? (0) | 2023.04.12 |
---|---|
SQL Server 구성 관리자를 찾을 수 없습니다. (0) | 2023.04.12 |
Oracle SQL에서 단일 견적 처리 방법 (0) | 2023.04.12 |
WPF에서 작업을 수행하기 전에 지연을 설정하는 방법 (0) | 2023.04.12 |
C# - Excel 상호 운용성 향상 (0) | 2023.04.12 |