WPF应用程序(.NET Core) 列表显示+分页+查询
WPF应用程序(.NET Core) 列表显示+分页+查询
一、列表显示+查询
效果:
1、启动页(App.xaml)中设置一个公共的列表头样式
1 2App.xaml
建立文件--- 右键=》添加 =》用户控件 =》生成窗体
2、.xaml文件
2-1 .xaml文件编辑布局
列表中ColumnHeaderStyle="{DynamicResource MyDataGridColumnHeadeStyle}"处使用了上面设置一个公共的列表头样式
1 xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" 2 3 4 Width="800" 5 Height="450" 6 7 8 9.Xmal"White"> 10 11 15 16 <DockPanel 17 Grid.Row="0" 18 Margin="15" 19 VerticalAlignment="Center"> 20"0.3*" /> 12 13 14 21 47 48 49 50 51 <DockPanel 52 Grid.Row="1" 53 Margin="15" 54 VerticalAlignment="Center"> 55 <DataGrid 56 x:Name="TenantDataGrid" 57 AutoGenerateColumns="False" 58 CanUserAddRows="False" 59 ColumnHeaderStyle="{DynamicResource MyDataGridColumnHeadeStyle}" 60 HeadersVisibility="All" 61 ItemsSource="{Binding AdminUserList}"> 6222 2623 24 25 "0" materialDesign:HintAssist.Hint="用户名"> 27 31 32 <ComboBox 33 Grid.Column="1" 34 Width="256" 35 materialDesign:HintAssist.Hint="是否启用" 36 materialDesign:TextFieldAssist.HasClearButton="True" 37 DisplayMemberPath="Value" 38 ItemsSource="{Binding Source={StaticResource IfUseEnum}, Path=ActiveEnum}" 39 SelectedValue="{Binding UserStatus}" 40 SelectedValuePath="Key" /> 41 42 <Button 43 Grid.Column="2" 44 Command="{Binding SearchClickCommand}" 45 Content="查询" /> 4628 30"UserName" UpdateSourceTrigger="PropertyChanged" /> 29 63 <DataGridTextColumn 64 Binding="{Binding user_name}" 65 EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" 66 ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}" 67 Header="登录用户名" /> 68 <DataGridTextColumn 69 Binding="{Binding user_name}" 70 EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" 71 ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}" 72 Header="用户姓名" /> 73 <DataGridTextColumn 74 Binding="{Binding user_status}" 75 EditingElementStyle="{StaticResource MaterialDesignDataGridTextColumnEditingStyle}" 76 ElementStyle="{StaticResource MaterialDesignDataGridTextColumnStyle}" 77 Header="用户状态" /> 78 96 97 98 99"*" Header="操作"> 79 94 9580 9381 <StackPanel 82 HorizontalAlignment="Center" 83 VerticalAlignment="Center" 84 Orientation="Horizontal"> 85 86 <Button 87 Command="{Binding Path=DataContext.OpenCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 88 CommandParameter="{Binding}" 89 Content="编辑" /> 90 91 92
2-2 上述中搜索条件下拉实现详述:自己声明的资源,用于提供搜索下拉数据(当前例子为枚举型)
1 ///IfUseEnum.cs2 /// 是否启用枚举 3 /// 4 public class IfUseEnum 5 { 6 //使用字典存入的值 7 public Dictionary<int, string> ActiveEnum { get; set; } = new Dictionary<int,string>() { {1, "启用" }, {0, "不启用" } }; 8 }
2-2-1 启动窗口(App.xaml)声明下,可以全局使用
2-2-2 xaml窗体页面中使用
完成下拉显示
2、对应.cs文件
1 public class UserManagerViewModel : BaseViewModel 2 { 3 private readonly IRegionManager regionManager; 4 private readonly LoginUserInfoService _LoginUserInfoService; 5 6 public UserManagerViewModel(IEventAggregator aggregators, IRegionManager regionManager, LoginUserInfoService _LoginUserInfoServices) : base(aggregators) 7 { 8 this.regionManager = regionManager; 9 this._LoginUserInfoService = _LoginUserInfoServices; 10 SearchClickCommand = new DelegateCommand(getUserInfo); 11 Ini();//初始化类似 12 } 13 14 //类似实现双向绑定--获取到查询的用户名 15 private string _userName; 16 public string UserName 17 { 18 get => _userName; set 19 { 20 _userName = value; 21 RaisePropertyChanged(); 22 } 23 } 24 //类似实现双向绑定--获取到查询的用户名状态 25 private int? _userStatus; 26 public int? UserStatus 27 { 28 get => _userStatus; set 29 { 30 _userStatus = value; 31 RaisePropertyChanged(); 32 } 33 } 34 35 ///ViewModel.cs36 /// 列表查询委托 37 /// 38 /// 查询按钮 39 public DelegateCommand SearchClickCommand { get; private set; } 40 41 private List _adminUserList; 42 /// 43 /// 用户列表数据 44 /// 45 public List AdminUserList 46 { 47 get => _adminUserList; set 48 { 49 _adminUserList = value; 50 RaisePropertyChanged(); 51 } 52 } 53 public void Ini() 54 { 55 //类似每次都需要初始化下 56 getUserInfo(); 57 } 58 //调用接口获取数据 59 public void getUserInfo() 60 { 61 //有查询条件需要放到serchModel种 62 SearchModel searchModel = new SearchModel(); 63 searchModel.SearchItem.Add("user_full_name", UserName);//存入字典中 64 if (UserStatus != null) 65 { 66 searchModel.SearchItem.Add("user_status",UserStatus.ToString()); 67 } 68 searchModel.PageIndex = 1; 69 searchModel.PageSize = 10; 70 var result = _LoginUserInfoService.LoginUserInfo(searchModel); 71 if (result == null) return; 72 AdminUserList = result.itemData; 73 } 74 /// 75 /// 列表编辑委托 76 /// 77 /// 编辑按钮 78 //public DelegateCommand OpenCommand { get; private set; } 79 }
3、对应服务类编辑--实现调用api接口获取数据(调用HttpRequestTools工具)
1 ///Service2 /// 调用api接口获取用户数据 3 /// 4 public class LoginUserInfoService : MyBaseService 5 { 6 //用户列表数据 7 public PagingModel LoginUserInfo(SearchModel s) 8 { 9 10 string url = "SystemBase/UserInfo/GetUserInfo"; 11 12 ResultModel > result = HttpRequestTools.PostByToken , SearchModel>(url, s); 13 if (result.code == ResultCode.Ok) 14 { 15 return result.data; 16 } 17 return new PagingModel (); 18 } 19 }
至此完成显示分页效果--api层当下没有编辑 ↑