一、原理及效果图
二、视图Model
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
public class StudentDao
{
List<Student> listStu = new List<Student>();
public int GetTotalCount
{
get
{
return listStu.Count;
}
}
public StudentDao()
{
for (int i = 1; i < 100; i++)
{
listStu.Add(new Student() { Id = i, Age = i, Name = "YY", Address = "湖南长沙" });
}
}
/// <summary>
/// 数据分页
/// </summary>
/// <param name="startNum">开始的索引</param>
/// <param name="count">数量</param>
/// <returns></returns>
public List<Student> GetListStu(int startNum, int count)
{
return listStu.Skip(startNum).Take(count).Cast<Student>().ToList();
}
}
三、实现命令接口
class CommandOperation : ICommand
{
private Action execute;
private Action<object> exrcuteParameter;
public CommandOperation(Action execute)
{
this.execute = execute;
}
public CommandOperation(Action<object> execute)
{
this.exrcuteParameter = execute;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
if (exrcuteParameter != null)
{
exrcuteParameter(parameter);
return;
}
execute();
}
}
四、ViewModel
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public static class PropertyChangedBaseEx
{
public static void NotifyPropertyChanged<T, TProperty>(this T propertyChangedBase,
Expression<Func<T, TProperty>> expression) where T : PropertyChangedBase
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
propertyChangedBase.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}
public class StuentViewModel : PropertyChangedBase
{
#region 通知属性
private int totalCount;
public int TotalCount
{
get { return totalCount; }
set
{
totalCount = value;
this.NotifyPropertyChanged(d => d.TotalCount);
}
}
private int pageCount;
public int PageCount
{
get { return pageCount; }
set
{
pageCount = value;
this.NotifyPropertyChanged(d => d.PageCount);
}
}
private int totalPage;
public int TotalPage
{
get { return totalPage; }
set
{
totalPage = value;
this.NotifyPropertyChanged(d => d.TotalPage);
}
}
private int currentPage;
public int CurrentPage
{
get { return currentPage; }
set
{
currentPage = value;
this.NotifyPropertyChanged(d => d.CurrentPage);
}
}
private int currentStart;
public int CurrentStart
{
get { return currentStart; }
set
{
currentStart = value;
this.NotifyPropertyChanged(d => d.CurrentStart);
}
}
private int currentEnd;
public int CurrentEnd
{
get { return currentEnd; }
set
{
currentEnd = value;
this.NotifyPropertyChanged(d => d.CurrentEnd);
}
}
private List<Student> listModel;
public List<Student> ListModel
{
get { return listModel; }
set
{
listModel = value;
this.NotifyPropertyChanged(d => d.ListModel);
}
}
#endregion
#region 命令属性
public ICommand CommNext { get; private set; }
public ICommand CommLast { get; private set; }
public ICommand CommFirst { get; private set; }
public ICommand CommPrev { get; private set; }
public ICommand CommPageCount { get; private set; }
#endregion
private StudentDao stuDao = new StudentDao();
public StuentViewModel()
{
CommFirst = new CommandOperation(First);
CommPrev = new CommandOperation(Prev);
CommNext = new CommandOperation(Next);
CommLast = new CommandOperation(Last);
//改变下拉列表索引 触发的命令
CommPageCount = new CommandOperation(parameter =>
{
PageCount = Convert.ToInt32(parameter);
Init();
});
TotalCount = stuDao.GetTotalCount;
PageCount = 20;
Init();
}
void Init()
{
TotalPage = (totalCount / pageCount) * pageCount < totalCount ? totalCount / pageCount + 1 : totalCount / pageCount;
CurrentPage = 1;
CurrentStart = 1;
CurrentEnd = PageCount > TotalCount ? TotalCount : PageCount;
ListModel = stuDao.GetListStu(currentStart - 1, currentEnd - currentStart + 1);
}
#region 分页方法
private void Next()
{
if (CurrentPage < TotalPage)
{
CurrentStart = CurrentPage * PageCount + 1;
CurrentEnd = CurrentStart + PageCount - 1 > TotalCount ? TotalCount : CurrentStart + PageCount - 1;
CurrentPage++;
ListModel = stuDao.GetListStu(currentStart - 1, currentEnd - currentStart + 1);
}
}
private void Last()
{
CurrentPage = TotalPage;
CurrentStart = TotalPage > 1 ? (CurrentPage - 1) * PageCount + 1 : 1;
CurrentEnd = TotalCount;
ListModel = stuDao.GetListStu(currentStart - 1, currentEnd - currentStart + 1);
}
private void Prev()
{
if (CurrentPage > 1)
{
CurrentPage--;
CurrentEnd = CurrentStart - 1;
CurrentStart = (CurrentPage - 1) * PageCount + 1;
ListModel = stuDao.GetListStu(currentStart - 1, currentEnd - currentStart + 1);
}
}
private void First()
{
CurrentPage = 1;
CurrentStart = 1;
CurrentEnd = PageCount > TotalCount ? TotalCount : PageCount;
ListModel = stuDao.GetListStu(currentStart - 1, currentEnd - currentStart + 1);
}
#endregion
}
五、界面绑定
<Grid x:Name="dataGrid">
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="MVVM DataGrid 数据分页 简单案例" FontSize="18" />
</StackPanel>
<DockPanel LastChildFill="True" Grid.Row="1">
<DataGrid ItemsSource="{Binding ListModel}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="序号" Binding="{Binding Id}" Width="2*"/>
<DataGridTextColumn Header="姓名" Binding="{Binding Name}" Width="2*"/>
<DataGridTextColumn Header="年龄" Binding="{Binding Age}" Width="2*"/>
<DataGridTextColumn Header="地址" Binding="{Binding Address}" Width="4*"/>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
<Grid Margin="0,10,0,0" Grid.Column="1" Grid.Row="3" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="240"/>
</Grid.ColumnDefinitions>
<ComboBox x:Name="combox1" Width="50" Height="20" SelectedIndex="0"
Margin="0,0,10,0" VerticalAlignment="Top" SelectionChanged="combox1_SelectionChanged" >
<ComboBoxItem>20</ComboBoxItem>
<ComboBoxItem>50</ComboBoxItem>
<ComboBoxItem>100</ComboBoxItem>
</ComboBox>
<WrapPanel Grid.Column="1" Grid.RowSpan="2" >
<WrapPanel >
<Button Width="80" Height="20" Command="{Binding CommFirst}" Content="第一页" />
<Button Width="80" Height="20" Command="{Binding CommPrev}" Content="前一页" Margin="10,0"/>
</WrapPanel>
<WrapPanel>
<TextBlock Text="当前第" VerticalAlignment="Center"/>
<TextBox Text="{Binding CurrentPage}" TextAlignment="Center" Width="20" Margin="2,0,0,2"/>
<TextBlock Text=" 页,共 " VerticalAlignment="Center" />
<TextBlock Foreground="Red" Text="{Binding TotalPage}" VerticalAlignment="Center"/>
<TextBlock Text=" 页" VerticalAlignment="Center" />
</WrapPanel>
<WrapPanel >
<Button Width="80" Height="20" Command="{Binding CommNext}" Content="下一页" Margin="10,0" />
<Button Width="80" Height="20" Command="{Binding CommLast}" Content="末页" Margin="0,0,10,0" />
</WrapPanel>
</WrapPanel>
<WrapPanel Grid.Column="2" Grid.RowSpan="2" >
<TextBlock Text="显示记录从 " VerticalAlignment="Center"/>
<TextBlock Text="{Binding CurrentStart}" Foreground="Red"/>
<TextBlock Text=" 到 "/>
<TextBlock Text="{Binding CurrentEnd}" Foreground="Red" />
<TextBlock Text=" 条, 总数 " />
<TextBlock Text="{Binding TotalCount}" Foreground="Red" />
<TextBlock Text=" 条" />
</WrapPanel>
</Grid>
</Grid>
六、设置DataContext 及处理下拉事件
public partial class MainWindow : Window
{
StuentViewModel viewModel = new StuentViewModel();
public MainWindow()
{
InitializeComponent();
dataGrid.DataContext = viewModel;
}
private void combox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string selObj = combox1.SelectedValue.ToString();
viewModel.CommPageCount.Execute(selObj.Split(':')[1]);
}
}
源码下载:http://download.youkuaiyun.com/detail/hyh834773759/5023766