WPF ViewModel分页代码
以下是一个简单的WPF ViewModel分页代码示例:
- ViewModel类
public class MyViewModel : INotifyPropertyChanged
{
private ObservableCollection<MyData> _dataList;
public ObservableCollection<MyData> DataList
{
get { return _dataList; }
set { _dataList = value; OnPropertyChanged("DataList"); }
}
private int _pageIndex;
public int PageIndex
{
get { return _pageIndex; }
set { _pageIndex = value; OnPropertyChanged("PageIndex"); }
}
private int _pageSize;
public int PageSize
{
get { return _pageSize; }
set { _pageSize = value; OnPropertyChanged("PageSize"); }
}
private int _totalItemCount;
public int TotalItemCount
{
get { return _totalItemCount; }
set { _totalItemCount = value; OnPropertyChanged("TotalItemCount"); }
}
private int _totalPageCount;
public int TotalPageCount
{
get { return _totalPageCount; }
set { _totalPageCount = value; OnPropertyChanged("TotalPageCount"); }
}
public MyViewModel()
{
PageSize = 10;
PageIndex = 1;
LoadData();
}
private void LoadData()
{
//TODO: 获取数据并赋值给DataList,同时更新TotalItemCount和TotalPageCount
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
- XAML页面
<Grid>
<DataGrid ItemsSource="{Binding DataList}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Id}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Age" Binding="{Binding Age}" />
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<Button Content="上一页" Margin="5" IsEnabled="{Binding PageIndex, Converter={StaticResource IsEnabledConverter}, ConverterParameter=Previous}" Command="{Binding PageIndex, Converter={StaticResource PageIndexConverter}, ConverterParameter=Previous}" />
<TextBlock Text="{Binding PageIndex}" Margin="5" />
<Button Content="下一页" Margin="5" IsEnabled="{Binding PageIndex, Converter={StaticResource IsEnabledConverter}, ConverterParameter=Next}" Command="{Binding PageIndex, Converter={StaticResource PageIndexConverter}, ConverterParameter=Next}" />
</StackPanel>
</Grid>
- Converter类
public class PageIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int pageIndex && parameter is string direction)
{
if (direction == "Previous")
{
return pageIndex > 1 ? pageIndex - 1 : 1;
}
else if (direction == "Next")
{
return pageIndex + 1;
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class IsEnabledConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is int pageIndex && parameter is string direction)
{
if (direction == "Previous")
{
return pageIndex > 1;
}
else if (direction == "Next")
{
return pageIndex < (int.MaxValue - 1);
}
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
- 注册Converter和ViewModel
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MyViewModel();
Resources.Add("PageIndexConverter", new PageIndexConverter());
Resources.Add("IsEnabledConverter", new IsEnabledConverter());
}
}
以上代码实现了一个简单的WPF ViewModel分页功能,通过点击上一页和下一页按钮实现数据的切换,同时显示当前页码。注意需要自行实现LoadData方法获取数据并更新DataList、TotalItemCount和TotalPageCount。
原文地址: https://www.cveoy.top/t/topic/bDk9 著作权归作者所有。请勿转载和采集!