wpf sqlsugar handycontrol mvvm分页
时间: 2023-10-14 14:04:48 浏览: 174
WPF是一种基于XAML的用户界面框架,用于创建桌面应用程序。SQLSugar是一个ORM框架,可以帮助开发人员更轻松地访问和管理数据库。HandyControl是一个WPF控件库,提供了一些常用的界面控件和样式。MVVM是一种设计模式,用于将应用程序的用户界面和业务逻辑分离开来,使得代码更加清晰易懂。
在WPF应用程序中使用SQLSugar可以帮助您更方便地访问和管理数据库数据。使用HandyControl可以快速地创建漂亮的用户界面,并且这些控件都可以轻松地与MVVM模式集成。分页功能可以帮助您将大量数据分成多个页面,使得用户可以更方便地浏览和查找数据。
下面是一个使用WPF、SQLSugar、HandyControl和MVVM分页的示例:
1. 创建一个WPF应用程序,并在项目中安装SQLSugar和HandyControl包。
2. 创建一个名为“Models”的文件夹,并在其中创建一个名为“Person.cs”的类,用于定义人员模型。
```csharp
public class Person
{
[SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
public int Id { get; set; }
[SugarColumn(Length = 50)]
public string Name { get; set; }
[SugarColumn(Length = 50)]
public string Gender { get; set; }
[SugarColumn(ColumnName = "BirthDate")]
public DateTime? DateOfBirth { get; set; }
[SugarColumn(Length = 50)]
public string Address { get; set; }
}
```
3. 创建一个名为“ViewModels”的文件夹,并在其中创建一个名为“PersonViewModel.cs”的类,用于定义人员视图模型。
```csharp
public class PersonViewModel : BaseViewModel
{
private int _pageIndex = 1;
private int _pageSize = 10;
private int _totalCount;
private List<Person> _personList;
private Person _selectedPerson;
public List<Person> PersonList
{
get { return _personList; }
set { SetProperty(ref _personList, value); }
}
public Person SelectedPerson
{
get { return _selectedPerson; }
set { SetProperty(ref _selectedPerson, value); }
}
public int PageIndex
{
get { return _pageIndex; }
set { SetProperty(ref _pageIndex, value); }
}
public int PageSize
{
get { return _pageSize; }
set { SetProperty(ref _pageSize, value); }
}
public int TotalCount
{
get { return _totalCount; }
set { SetProperty(ref _totalCount, value); }
}
public ICommand FirstPageCommand { get; private set; }
public ICommand PreviousPageCommand { get; private set; }
public ICommand NextPageCommand { get; private set; }
public ICommand LastPageCommand { get; private set; }
public PersonViewModel()
{
FirstPageCommand = new RelayCommand(FirstPage);
PreviousPageCommand = new RelayCommand(PreviousPage);
NextPageCommand = new RelayCommand(NextPage);
LastPageCommand = new RelayCommand(LastPage);
LoadData();
}
private void LoadData()
{
using (var db = new SqlSugarClient(new ConnectionConfig { ConnectionString = "your connection string" }))
{
var query = db.Queryable<Person>().OrderBy(p => p.Id);
TotalCount = query.Count();
PersonList = query.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
}
}
private void FirstPage()
{
PageIndex = 1;
LoadData();
}
private void PreviousPage()
{
if (PageIndex > 1)
{
PageIndex--;
LoadData();
}
}
private void NextPage()
{
if (PageIndex < TotalCount / PageSize + 1)
{
PageIndex++;
LoadData();
}
}
private void LastPage()
{
PageIndex = TotalCount / PageSize + 1;
LoadData();
}
}
```
4. 创建一个名为“Views”的文件夹,并在其中创建一个名为“PersonView.xaml”的视图,用于显示人员数据和分页控件。
```xml
<UserControl x:Class="WpfApp.Views.PersonView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="clr-namespace:HandyControl.Controls;assembly=HandyControl"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:vm="clr-namespace:WpfApp.ViewModels"
DataContext="{Binding PersonViewModel, Source={StaticResource Locator}}">
<Grid>
<StackPanel>
<DataGrid ItemsSource="{Binding PersonList}" SelectedItem="{Binding SelectedPerson}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Gender}"/>
<DataGridTextColumn Header="Date of Birth" Binding="{Binding DateOfBirth}"/>
<DataGridTextColumn Header="Address" Binding="{Binding Address}"/>
</DataGrid.Columns>
</DataGrid>
<hc:Pager CurrentPage="{Binding PageIndex}" PageSize="{Binding PageSize}" TotalCount="{Binding TotalCount}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="FirstPageClick">
<i:InvokeCommandAction Command="{Binding FirstPageCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="PreviousPageClick">
<i:InvokeCommandAction Command="{Binding PreviousPageCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="NextPageClick">
<i:InvokeCommandAction Command="{Binding NextPageCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="LastPageClick">
<i:InvokeCommandAction Command="{Binding LastPageCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</hc:Pager>
</StackPanel>
</Grid>
</UserControl>
```
5. 在App.xaml中添加以下代码,以便在应用程序中使用ViewModelLocator。
```xml
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>
</Application.Resources>
```
6. 在MainWindow.xaml中使用PersonView。
```xml
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views="clr-namespace:WpfApp.Views"
Title="MainWindow" Height="450" Width="800">
<Grid>
<views:PersonView/>
</Grid>
</Window>
```
现在您已经可以运行应用程序并使用分页控件浏览人员数据了。
阅读全文