背景:最近在写项目的时候需要写一个简单的分页功能,因项目需要,没有改为MVVM模式,只需要在后台实现
1、呈现效果如下:
接下来就来上代码,看看怎么实现的
1、界面代码
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Right">
<TextBlock TextWrapping="WrapWithOverflow"
HorizontalAlignment="Right"
Margin="5,0,5,0"
VerticalAlignment="Center"
Text="{Binding OrderCount}">
</TextBlock>
<Button ToolTip="上一页"
Width=""
Height=""
FontSize=""
Name="BtnPreviousPage"
Background="#FFFFFF"
Click="BtnPreviousPage_Click"
Margin="5,0,5,0">
</Button>
<Border BorderBrush="#009639"
BorderThickness=""
Width=""
Height=""
Margin="5,0,5,0">
<TextBlock TextWrapping="WrapWithOverflow"
Text="{Binding CurrentPage}"
Foreground="#009639"
VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
<Button ToolTip="下一页"
Width=""
Height=""
FontSize=""
Background="#FFFFFF"
Name="BtnNextPage"
Click="BtnNextPage_Click"
Margin="5,0,5,0">
</Button>
</StackPanel>
界面代码
2、后台代码
private string _amountMoney; private string _orderCount; private int _currentPage = ; private int _pageSize = ; /// <summary>
/// 未上传订单总数
/// </summary>
private int _count; List<OrderJMSStatus> mList = new List<OrderJMSStatus>(); public int CurrentPage
{
get { return _currentPage; }
set
{
if (_currentPage != value)
{
_currentPage = value;
RaisePropertyChanged("CurrentPage");
}
}
} public string OrderCount
{
get { return _orderCount; }
set
{
if (_orderCount != value)
{
_orderCount = value;
RaisePropertyChanged("OrderCount");
}
}
} public string AmountMoney
{
get { return _amountMoney; }
set
{
if (_amountMoney != value)
{
_amountMoney = value;
RaisePropertyChanged("AmountMoney");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private int GetPageNumber(int count, int rowNumber)
{
if (count == || rowNumber == )
{
return ;
}
int pageNumber = count / rowNumber;
if (count % rowNumber != )
{
pageNumber++;
}
return pageNumber;
} private void BtnNextPage_Click(object sender, RoutedEventArgs e)
{ if (CurrentPage == GetPageNumber(_count, _pageSize))
{
return;
} List<OrderJMSStatus> result = mList.Skip(CurrentPage * _pageSize).Take(_pageSize).ToList(); MessageList.Clear();
MessageList = result;
CurrentPage++;
} private void BtnPreviousPage_Click(object sender, RoutedEventArgs e)
{
if (CurrentPage == )
{
return;
} List<OrderJMSStatus> result = new List<OrderJMSStatus>(); if (CurrentPage == )
{
result = mList.Take(_pageSize).ToList();
}
else
{
result = mList.Skip((CurrentPage - ) * _pageSize).Take(_pageSize).ToList();
} MessageList.Clear();
MessageList = result; CurrentPage--;
}
后台代码
实现过程基本上就是这样,希望能对你有所帮助