WPF MVVM 分页实现:一个简单示例
以下是一个简单的WPF MVVM分页示例:
- 首先,创建一个名为'PagingViewModel'的ViewModel类,其中包含以下属性和命令:
public class PagingViewModel : INotifyPropertyChanged
{
private ObservableCollection<string> _items;
private int _currentPage;
private int _totalPages;
private ICommand _previousPageCommand;
private ICommand _nextPageCommand;
public ObservableCollection<string> Items
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged(nameof(Items));
}
}
public int CurrentPage
{
get { return _currentPage; }
set
{
_currentPage = value;
OnPropertyChanged(nameof(CurrentPage));
}
}
public int TotalPages
{
get { return _totalPages; }
set
{
_totalPages = value;
OnPropertyChanged(nameof(TotalPages));
}
}
public ICommand PreviousPageCommand
{
get { return _previousPageCommand ?? (_previousPageCommand = new RelayCommand(PreviousPage, CanGoToPreviousPage)); }
}
public ICommand NextPageCommand
{
get { return _nextPageCommand ?? (_nextPageCommand = new RelayCommand(NextPage, CanGoToNextPage)); }
}
//Methods for navigating to previous and next pages
private void PreviousPage()
{
CurrentPage--;
UpdateItems();
}
private void NextPage()
{
CurrentPage++;
UpdateItems();
}
//Methods for checking if navigation to previous and next pages is possible
private bool CanGoToPreviousPage()
{
return CurrentPage > 1;
}
private bool CanGoToNextPage()
{
return CurrentPage < TotalPages;
}
//Method for updating the collection of items based on the current page number
private void UpdateItems()
{
Items.Clear();
//TODO: Populate the Items collection based on the current page number
}
//INotifyPropertyChanged implementation
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
- 在MainWindow.xaml中定义界面布局。这里使用Grid控件来布局,并在其中添加一个ListBox控件来显示分页数据。
<Window x:Class="PagingExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PagingExample"
Title="Paging Example" Height="350" Width="525">
<Window.DataContext>
<local:PagingViewModel />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" ItemsSource="{Binding Items}" />
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Previous" Command="{Binding PreviousPageCommand}" />
<TextBlock Text="{Binding CurrentPage}" Margin="5" />
<Button Content="Next" Command="{Binding NextPageCommand}" />
</StackPanel>
</Grid>
</Window>
- 在ViewModel类的构造函数中初始化分页数据,并计算总页数。
public PagingViewModel()
{
Items = new ObservableCollection<string>();
//TODO: Populate the Items collection with initial data
CurrentPage = 1;
TotalPages = (int)Math.Ceiling((double)Items.Count / PageSize);
}
- 在UpdateItems方法中根据当前页码和页大小计算要显示的数据,并更新Items集合。
private void UpdateItems()
{
Items.Clear();
int startIndex = (CurrentPage - 1) * PageSize;
int endIndex = Math.Min(startIndex + PageSize, Items.Count);
for (int i = startIndex; i < endIndex; i++)
{
Items.Add(Items[i]);
}
}
- 在RelayCommand类中定义一个简单的ICommand实现。
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public RelayCommand(Action execute, Func<bool> canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute?.Invoke() ?? true;
}
public void Execute(object parameter)
{
_execute();
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
- 最后,在App.xaml.cs中启动应用程序,并在Main方法中设置主窗口。
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
}
这样就完成了一个简单的WPF MVVM分页示例。
原文地址: https://www.cveoy.top/t/topic/l6HG 著作权归作者所有。请勿转载和采集!