C#WPF实战出真汁06--【系统设置】--餐桌类型设置
1、系统设置的基本概念
系统设置是用于配置和管理餐桌类型和菜品类型,是维护整个系统的基础数据。通过系统设置,用户可以调整餐桌类型的添加,删除,编辑,分页,查询,重置,列表,选择等。
2、布局设计
餐桌类型设置及菜品类型设置是通过TabControl控件实现的。不过要注意的是,左侧每个菜单点击时,右侧呈现出来的是用户控件,是用户控件,而不是窗体。
1、控件介绍
TabControl 是一种功能强大且灵活的控件,适用于需要分页显示内容的场景。通过合理使用其属性和事件,可以创建出满足各种需求的用户界面。TabControl 是一种常见的用户界面控件,用于在多个选项卡之间切换内容。它通常由一组标签页(TabPage)组成,每个标签页包含不同的内容,用户可以通过点击标签页头来切换显示的内容。在 WPF 中,TabControl 的使用方式更加灵活,通常与数据绑定结合使用。以下是 XAML 示例:
<TabControl><TabItem Header="选项卡1"><Label Content="这是第一个选项卡的内容"/></TabItem><TabItem Header="选项卡2"><Label Content="这是第二个选项卡的内容"/></TabItem>
</TabControl>
TabControl 的事件,TabControl 提供了多个事件用于响应用户操作,以下是常见事件:
SelectedIndexChanged:当选中标签页发生变化时触发。该事件本项目中必须用到
Selecting:在选中标签页之前触发,可以取消选中操作。
Selected:在选中标签页之后触发。
2、布局应用
完整代码如下:
<UserControl x:Class="HQ.fResApp.UControls.XiTongSheZhi"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:HQ.fResApp.UControls"mc:Ignorable="d" xmlns:vm="clr-namespace:HQ.fResApp.ViewModel" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"xmlns:pu="clr-namespace:Panuon.UI.Silver;assembly=Panuon.UI.Silver"Padding="20"d:DesignHeight="450"d:DesignWidth="1000"Background="#f6f9ff"><UserControl.DataContext><vm:DiningDishTypeVModel/></UserControl.DataContext><TabControlMargin="10"VerticalAlignment="Stretch"pu:TabControlHelper.HeaderPanelBackground="#fcfcfc"pu:TabControlHelper.ItemHeight="50"pu:TabControlHelper.ItemPadding="20,0"pu:TabControlHelper.ItemsAlignment="LeftOrTop"pu:TabControlHelper.SelectedBackground="#FF009BFF"pu:TabControlHelper.SelectedForeground="#ffffff" pu:TabControlHelper.TabControlStyle="Card"><TabItem Cursor="Hand" Header="餐桌类型设置" Background="Aquamarine" BorderThickness="1" FontSize="20" BorderBrush="AliceBlue" FontFamily="黑体" FontStyle="Normal" ><Grid Margin="10"><Grid.RowDefinitions><RowDefinition Height="55" /><RowDefinition Height="15" /><RowDefinition /><RowDefinition Height="73" /></Grid.RowDefinitions><Grid Background="#ffffff"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="460" /></Grid.ColumnDefinitions><StackPanel Orientation="Horizontal" Grid.ColumnSpan="2" Margin="0,0,410,0"><TextBoxx:Name="keyWhereTab"Width="200"Height="40"Margin="0,15,20,0"pu:TextBoxHelper.Watermark="输入餐桌关键字"FontSize="15"Foreground="#909399"Text="{Binding DiningTypeKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /><Button Width="130"Height="40"Margin="0,10,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#009BFF"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/search.png"pu:IconHelper.Width="40"Background="#FF009BFF"BorderBrush="#FF009BFF" Content="查询"Cursor="Hand"FontSize="16"Foreground="#ffffff"Command="{Binding FindCommand}" CommandParameter="dining"IsDefault="true" /><Button Width="130"Height="40"Margin="0,10,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#009BFF"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/Refresh.png"pu:IconHelper.Width="40"Background="LightSlateGray"BorderBrush="#FF009BFF" Content="重置"Cursor="Hand"FontSize="16"Foreground="#ffffff"Command="{Binding ResetCommand}" CommandParameter="dining" /></StackPanel><StackPanelGrid.Column="1"HorizontalAlignment="Right"Orientation="Horizontal"><Buttonx:Name="btnAddTab"Width="130"Height="40"Margin="0,10,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#65d17f"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/add.png"pu:IconHelper.Width="35"Background="#65d17f"BorderBrush="#65d17f" Content="新增"Cursor="Hand"FontSize="16"Foreground="#ffffff" /><Buttonx:Name="btnDelTab"Width="130"Height="40"Margin="0,10,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#FF5722"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/remove1.png"pu:IconHelper.Width="35"Background="#FF5722"BorderBrush="#FF5722" Content="删除"Cursor="Hand"FontSize="16"Foreground="#ffffff" /></StackPanel></Grid><Border Grid.Row="1" /><DataGridx:Name="dataListTab"Grid.Row="2"pu:DataGridHelper.ColumnHorizontalContentAlignment="Center"pu:DataGridHelper.HeaderBackground="#FF009BFF"pu:DataGridHelper.HeaderForeground="#ffffff"pu:DataGridHelper.HeaderMinHeight="50"pu:DataGridHelper.HoverBackground="#FF009BFF"pu:DataGridHelper.ResizeThumbThickness="0.5"pu:DataGridHelper.SelectedBackground="Transparent"AlternatingRowBackground="#f7faff"AutoGenerateColumns="False"CanUserAddRows="False"CanUserDeleteRows="False"CanUserReorderColumns="False"CanUserResizeRows="False"FontSize="16"ItemsSource="{Binding DiningTypeList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"SelectionMode="Extended"SelectionUnit="FullRow"><DataGrid.RowStyle><Style TargetType="{x:Type DataGridRow}"><EventSetter Event="UIElement.GotFocus" Handler="TabItem_GotFocus" /><Setter Property="Height" Value="40" /></Style></DataGrid.RowStyle><DataGrid.Columns><DataGridCheckBoxColumn x:Name="chkrtIdTab" CanUserSort="False"><DataGridCheckBoxColumn.HeaderTemplate><DataTemplate><CheckBoxx:Name="ckbSelectedAll"pu:CheckBoxHelper.CheckBoxStyle="Switch"Checked="ckbSelectedAllTab_Checked"Content="全选"Foreground="#ffffff"IsChecked="False"Unchecked="ckbSelectedAllTab_Unchecked" /></DataTemplate></DataGridCheckBoxColumn.HeaderTemplate></DataGridCheckBoxColumn><DataGridTextColumnWidth="2*"Binding="{Binding rtName}"Header="餐桌类型名称"IsReadOnly="True" /><DataGridTextColumnWidth="2*"Binding="{Binding rtLeastCost}"Header="最低消费"IsReadOnly="True" /><DataGridTextColumnWidth="1*"Binding="{Binding rtMostNumber}"Header="容纳人数"IsReadOnly="True" /><DataGridTextColumnWidth="1*"Binding="{Binding rtAmount}"Header="房间个数"IsReadOnly="True" /><DataGridTextColumnWidth="3*"Binding="{Binding createDate, ConverterCulture=zh-CN, StringFormat=\{0:yyyy年MM月dd日 dddd HH时mm分\}}"Header="创建日期"IsReadOnly="True" /></DataGrid.Columns></DataGrid><Grid Grid.Row="3"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="250" /></Grid.ColumnDefinitions><!--餐桌类型分页控件--><pu:Paginationx:Name="tabPaginationTab"Height="45"Margin="0,0,20,0"HorizontalAlignment="Left"Background="#963F3F3F"CurrentIndexChanged="tabPaginationTab_CurrentIndexChanged"Cursor="Hand"HoverBrush="#FF009BFF"Spacing="15" CurrentIndex="{Binding DiningCurrentIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TotalIndex="{Binding DiningTotalIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /><StackPanelGrid.Column="1"HorizontalAlignment="Right"Orientation="Horizontal"><ButtonWidth="20"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="共"FontSize="17"FontWeight="ExtraBold"Foreground="#2F4056" /><Buttonx:Name="txtTotalNumTab"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="{Binding DiningTotalNum,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"FontSize="17"FontWeight="ExtraBold"Foreground="#FF009BFF" /><ButtonWidth="100"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="条数据/每页"FontSize="17"FontWeight="ExtraBold"Foreground="#2F4056" /><TextBoxx:Name="txtPageSizeTab"Grid.Row="2"Width="30"Height="30"HorizontalAlignment="Center"pu:TextBoxHelper.CornerRadius="0"Text="{Binding DiningPageSize,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /><ButtonWidth="20"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="条"FontSize="17"FontWeight="ExtraBold"Foreground="#2F4056" /></StackPanel></Grid></Grid></TabItem><TabItem Cursor="Hand" Header="菜品类型设置" Background="Aquamarine" BorderThickness="1" FontSize="20" BorderBrush="AliceBlue" FontFamily="黑体" FontStyle="Normal" ><Grid Margin="10"><Grid.ColumnDefinitions><ColumnDefinition Width="27*"/><ColumnDefinition Width="443*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinition Height="55" /><RowDefinition Height="15" /><RowDefinition /><RowDefinition Height="73" /></Grid.RowDefinitions><Grid Background="#ffffff" Grid.ColumnSpan="2"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="460" /></Grid.ColumnDefinitions><StackPanel Orientation="Horizontal" Grid.ColumnSpan="2" Margin="0,0,385,0"><TextBoxx:Name="keyPWhere"Width="200"Height="40"Margin="20,15,20,0"pu:TextBoxHelper.Watermark="输入菜品关键字"FontSize="15"Foreground="#909399"Text="{Binding DishTypeKey,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /> <Buttonx:Name="btnPSearch"Width="130"Height="40"Margin="0,15,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#009BFF"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/search.png"pu:IconHelper.Width="40"Background="#FF009BFF"BorderBrush="#FF009BFF" Content="查询"Cursor="Hand"FontSize="16"Foreground="#ffffff"Command="{Binding FindCommand}" CommandParameter="dish"IsDefault="true" /><Button Width="130"Height="40"Margin="0,10,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#009BFF"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/Refresh.png"pu:IconHelper.Width="40"Background="LightSlateGray"BorderBrush="#FF009BFF" Content="重置"Cursor="Hand"FontSize="16"Foreground="#ffffff"Command="{Binding ResetCommand}" CommandParameter="dish" /></StackPanel><StackPanelGrid.Column="1"HorizontalAlignment="Right"Orientation="Horizontal"><Buttonx:Name="btnPAdd"Width="130"Height="40"Margin="0,15,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#65d17f"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/add.png"pu:IconHelper.Width="35"Background="#65d17f"BorderBrush="#65d17f" Content="新增"Cursor="Hand"FontSize="16"Foreground="#ffffff" /><Buttonx:Name="btnPDel"Width="130"Height="40"Margin="0,15,20,0"Padding="-10,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.ClickStyle="Sink"pu:ButtonHelper.CornerRadius="20"pu:ButtonHelper.HoverBrush="#FF5722"pu:ButtonHelper.Icon="/HQ.fResApp;component/Resources/icon/remove1.png" pu:IconHelper.Width="35"Background="#FF5722"BorderBrush="#FF5722" Content="删除"Cursor="Hand"FontSize="16"Foreground="#ffffff" /></StackPanel></Grid><Border Grid.Row="1" Grid.ColumnSpan="2" /><DataGridx:Name="dataPList"Grid.Row="2"pu:DataGridHelper.ColumnHorizontalContentAlignment="Center"pu:DataGridHelper.HeaderBackground="#FF009BFF"pu:DataGridHelper.HeaderForeground="#ffffff"pu:DataGridHelper.HeaderMinHeight="50"pu:DataGridHelper.HoverBackground="#FF009BFF"pu:DataGridHelper.ResizeThumbThickness="0.5"pu:DataGridHelper.SelectedBackground="Transparent"AlternatingRowBackground="#f7faff"AutoGenerateColumns="False"CanUserAddRows="False"CanUserDeleteRows="False"CanUserReorderColumns="False"CanUserResizeRows="False"FontSize="16"ItemsSource="{Binding DishTypeList,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"SelectionMode="Extended"SelectionUnit="FullRow" Grid.ColumnSpan="2"><DataGrid.RowStyle><Style TargetType="{x:Type DataGridRow}"><EventSetter Event="UIElement.GotFocus" Handler="ProItem_GotFocus" /><Setter Property="Height" Value="40" /></Style></DataGrid.RowStyle><DataGrid.Columns><DataGridCheckBoxColumn x:Name="chkrtPId" CanUserSort="False"><DataGridCheckBoxColumn.HeaderTemplate><DataTemplate><CheckBoxx:Name="ckbSelectedAll"pu:CheckBoxHelper.CheckBoxStyle="Switch"Checked="ckbSelectedAllPro_Checked"Content="全选"Foreground="#ffffff"IsChecked="False"Unchecked="ckbSelectedAllPro_Unchecked" /></DataTemplate></DataGridCheckBoxColumn.HeaderTemplate></DataGridCheckBoxColumn><DataGridTextColumnWidth="3*"Binding="{Binding mtName}"Header="菜品类型名称"IsReadOnly="True" /><DataGridTextColumnWidth="3*"Binding="{Binding createDate, ConverterCulture=zh-CN, StringFormat=\{0:yyyy年MM月dd日 dddd HH时mm分\}}"Header="创建日期"IsReadOnly="True" /></DataGrid.Columns></DataGrid><Grid Grid.Row="3" Grid.ColumnSpan="2"><Grid.ColumnDefinitions><ColumnDefinition /><ColumnDefinition Width="250" /></Grid.ColumnDefinitions><pu:Paginationx:Name="tabPPagination"Height="45"Margin="0,0,20,0"HorizontalAlignment="Left"Background="#963F3F3F"CurrentIndexChanged="ProPagination_CurrentIndexChanged"Cursor="Hand"HoverBrush="#FF009BFF"Spacing="15"CurrentIndex="{Binding DishCurrentIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" TotalIndex="{Binding DishTotalIndex,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /><StackPanelGrid.Column="1"HorizontalAlignment="Right"Orientation="Horizontal"><ButtonWidth="20"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="共"FontSize="17"FontWeight="ExtraBold"Foreground="#2F4056" /><Buttonx:Name="txtPTotalNum"Content="{Binding DishTotalNum,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent" FontSize="17"FontWeight="ExtraBold"Foreground="#FF009BFF" /><ButtonWidth="100"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="条数据/每页"FontSize="17"FontWeight="ExtraBold"Foreground="#2F4056" /><TextBoxx:Name="txtPPageSize"Grid.Row="2"Width="25"Height="30"HorizontalAlignment="Center"pu:TextBoxHelper.CornerRadius="0"Text="{Binding DishPageSize,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" /><ButtonWidth="20"Height="45"Padding="-35,0,0,0"pu:ButtonHelper.ButtonStyle="Standard"pu:ButtonHelper.HoverBrush="Transparent"pu:IconHelper.Width="35"Background="Transparent"BorderBrush="Transparent"Content="条"FontSize="17"FontWeight="ExtraBold"Foreground="#2F4056" /></StackPanel></Grid></Grid></TabItem> </TabControl>
</UserControl>
Panuon.UI.Silver控件库中带有分页控件Pagination,这个分页控件蛮好用,灵活方便好使,支持随时修改每页条数,自动更新分页导航。
3、视图模型
在WPF中,ViewModel
是MVVM(Model-View-ViewModel)架构模式的核心组件,负责连接View
(界面)和Model
(数据/业务逻辑)。
ViewModel的核心作用
数据绑定桥梁:通过属性暴露数据,供View双向绑定,实现数据自动同步。
命令处理:封装UI操作逻辑,通过ICommand接口实现事件响应(如按钮点击)。
状态管理:维护View的视觉状态(如加载中、错误提示等)。
1、查询命令
2、重置命令
完整代码如下:
using HQ.BLL;
using HQ.COMM;
using HQ.fResApp.BaseModel;
using HQ.fResApp.Utils;
using HQ.MODEL.DBModel;
using HQ.MODEL.UIModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Controls;
using System.Windows.Input;namespace HQ.fResApp.ViewModel
{/// <summary>/// 餐桌类型和菜品类型viewmodel/// </summary>public class DiningDishTypeVModel : ViewModelBase{DiningTypeBLL diningTypebll = new DiningTypeBLL();DishTypeBLL dishTypebll = new DishTypeBLL();#region 餐桌类型页面属性private ObservableCollection<DiningType> diningTypeList;/// <summary>/// 餐桌类型列表,页面绑定的数据源/// </summary>public ObservableCollection<DiningType> DiningTypeList{get { return diningTypeList; }set{diningTypeList = value;OnPropertyChanged();}}private string diningTypeKey="";/// <summary>/// 餐桌关键字/// </summary>public string DiningTypeKey{get { return diningTypeKey; }set{diningTypeKey = value;OnPropertyChanged();}} private int diningCurrentIndex=1;/// <summary>/// 餐桌类型当前页/// </summary>public int DiningCurrentIndex{get { return diningCurrentIndex; }set{diningCurrentIndex = value;OnPropertyChanged();}}private int diningTotalIndex;/// <summary>/// 餐桌类型总页数/// </summary>public int DiningTotalIndex{get { return diningTotalIndex; }set{diningTotalIndex = value;OnPropertyChanged();}} private int diningTotalNum;/// <summary>/// 餐桌类型总条数/// </summary>public int DiningTotalNum{get { return diningTotalNum; }set{diningTotalNum = value;OnPropertyChanged();}}private int diningPageSize=10;/// <summary>/// 餐桌类型每页条数/// </summary>public int DiningPageSize{get { return diningPageSize; }set{diningPageSize = value;OnPropertyChanged();DiningTypeList = GetDingTypeTable();}}#endregionpublic DiningDishTypeVModel(){DiningTypeList = GetDingTypeTable(); }/// <summary>/// 获取餐桌类型列表/// </summary>/// <returns></returns>public ObservableCollection<DiningType> GetDingTypeTable(){ObservableCollection<DiningType> tableDataList = new ObservableCollection<DiningType>(); var _parms = new PageParm { page = DiningCurrentIndex, limit = DiningPageSize, key = DiningTypeKey.Trim() }; var _pageRes = diningTypebll.GetPagesAsync(_parms).Result;//分页查询if (_pageRes.statusCode == (int)ApiEnum.Status){var _pageResData = _pageRes.data;var _tabList = _pageResData.Items;if (_tabList != null&&_tabList.Count!=0){ foreach (var item in _tabList){var _curTab = new DiningType{rtGuid = item.rtGuid,rtName = item.rtName,createDate = item.createDate};tableDataList.Add(_curTab);} DiningTotalNum = (int)_pageResData.TotalItems;DiningCurrentIndex = (int)_pageResData.CurrentPage;DiningTotalIndex = (int)_pageResData.TotalPages;}else{Notice.Show("没有获取到餐桌数据!", "提示", 3, MessageBoxIcon.Info);Logger.Default.ProcessError((int)ApiEnum.Error, "没有获取到餐桌列表数据");}}else{Notice.Show("没有获取到餐桌数据!", "提示", 3, MessageBoxIcon.Info);Logger.Default.ProcessError(_pageRes.statusCode, "获取到餐桌类型列表数据异常");}return tableDataList; }/// <summary>/// 查询按钮的命令处理/// </summary>public ICommand FindCommand{get{return new RelayCommand(o =>{string typeobj = o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":DiningTypeList = GetDingTypeTable();break;}});}}/// <summary>/// 重置按钮的命令处理/// </summary>public ICommand ResetCommand{get{return new RelayCommand(o =>{string typeobj = o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":DiningCurrentIndex = 1;DiningPageSize = 10;DiningTypeKey = "";DiningTypeList = GetDingTypeTable();break;}});}}}
}
3、新增命令
4、编辑命令
5、完整代码
using HQ.BLL;
using HQ.Comm;
using HQ.COMM;
using HQ.fResApp.BaseModel;
using HQ.fResApp.Utils;
using HQ.fResApp.ViewModel.PageViewModel;
using HQ.fResApp.XWindows;
using HQ.MODEL.DBModel;
using HQ.MODEL.UIModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Xml.Linq;namespace HQ.fResApp.ViewModel
{/// <summary>/// 餐桌类型和菜品类型viewmodel/// </summary>public class DiningDishTypeListVModel : ViewModelBase{DiningTypeBLL diningTypebll = new DiningTypeBLL();DishTypeBLL dishTypebll = new DishTypeBLL();#region 餐桌类型页面属性private ObservableCollection<DiningTypeCheckInfo> diningTypeList;/// <summary>/// 餐桌类型列表,页面绑定的数据源/// </summary>public ObservableCollection<DiningTypeCheckInfo> DiningTypeList{get { return diningTypeList; }set{diningTypeList = value;OnPropertyChanged();}}private string diningTypeKey="";/// <summary>/// 餐桌关键字/// </summary>public string DiningTypeKey{get { return diningTypeKey; }set{diningTypeKey = value;OnPropertyChanged();}} private int diningCurrentIndex=1;/// <summary>/// 餐桌类型当前页/// </summary>public int DiningCurrentIndex{get { return diningCurrentIndex; }set{diningCurrentIndex = value;OnPropertyChanged();}}private int diningTotalIndex;/// <summary>/// 餐桌类型总页数/// </summary>public int DiningTotalIndex{get { return diningTotalIndex; }set{diningTotalIndex = value;OnPropertyChanged();}} private int diningTotalNum;/// <summary>/// 餐桌类型总条数/// </summary>public int DiningTotalNum{get { return diningTotalNum; }set{diningTotalNum = value;OnPropertyChanged();}}private int diningPageSize=10;/// <summary>/// 餐桌类型每页条数/// </summary>public int DiningPageSize{get { return diningPageSize; }set{diningPageSize = value;OnPropertyChanged();DiningTypeList = GetDingTypeTable();}}private bool isCheckAllDining = false;/// <summary>/// 餐桌全选状态/// </summary>public bool IsCheckAllDining{get { return isCheckAllDining; }set{isCheckAllDining = value;OnPropertyChanged();}}#endregion#region 菜品类型页面属性private ObservableCollection<DishTypeCheckInfo> dishTypeList;/// <summary>/// 菜品类型列表,页面绑定的数据源/// </summary>public ObservableCollection<DishTypeCheckInfo> DishTypeList{get { return dishTypeList; }set{dishTypeList = value;OnPropertyChanged();}}private string dishTypeKey="";/// <summary>/// 菜品关键字/// </summary>public string DishTypeKey{get { return dishTypeKey; }set{dishTypeKey = value;OnPropertyChanged();}}private int dishCurrentIndex = 1;/// <summary>/// 菜品类型当前页/// </summary>public int DishCurrentIndex{get { return dishCurrentIndex; }set{dishCurrentIndex = value;OnPropertyChanged();}}private int dishTotalIndex;/// <summary>/// 菜品类型总页数/// </summary>public int DishTotalIndex{get { return dishTotalIndex; }set{dishTotalIndex = value;OnPropertyChanged();}}private int dishTotalNum;/// <summary>/// 菜品类型总条数/// </summary>public int DishTotalNum{get { return dishTotalNum; }set{dishTotalNum = value;OnPropertyChanged();}}private int dishPageSize =10;/// <summary>/// 菜品类型每页条数/// </summary>public int DishPageSize{get { return dishPageSize; }set{dishPageSize = value;OnPropertyChanged();DishTypeList = GetDishTypeTable();}}private bool isCheckAllDish = false;/// <summary>/// 餐桌全选状态/// </summary>public bool IsCheckAllDish{get { return isCheckAllDish; }set{isCheckAllDish = value;OnPropertyChanged();}}#endregionpublic DiningDishTypeListVModel(){DiningTypeList = GetDingTypeTable();DishTypeList = GetDishTypeTable();}/// <summary>/// 获取菜品类型列表/// </summary>/// <returns></returns>public ObservableCollection<DishTypeCheckInfo> GetDishTypeTable(){ObservableCollection<DishTypeCheckInfo> tableDataList = new ObservableCollection<DishTypeCheckInfo>(); var _parms = new PageParm { page = DishCurrentIndex, limit = DishPageSize, key = DishTypeKey.Trim() };var _pageRes = dishTypebll.GetPagesAsync(_parms).Result;//分页查询if (_pageRes.statusCode == (int)ApiEnum.Status){var _pageResData = _pageRes.data;var _tabList = _pageResData.Items;if (_tabList != null && _tabList.Count != 0){ObservableCollection<DishTypeCheckInfo> proDataList = new ObservableCollection<DishTypeCheckInfo>();foreach (var item in _tabList){ var _curTab = new DishTypeCheckInfo();_curTab.DishTypeInfo.mtGuid = item.mtGuid;_curTab.DishTypeInfo.mtName = item.mtName;_curTab.DishTypeInfo.createDate = item.createDate;tableDataList.Add(_curTab);} DishTotalNum = (int)_pageResData.TotalItems ;DishCurrentIndex = (int)_pageResData.CurrentPage;DishTotalIndex = (int)_pageResData.TotalPages;}else{Notice.Show("没有获取到菜品类型数据!", "提示", 3, MessageBoxIcon.Info);Logger.Default.ProcessError((int)ApiEnum.Error, "没有获取到菜品类型列表数据");}}else{Notice.Show("没有获取到菜品类型数据!", "提示", 3, MessageBoxIcon.Info);Logger.Default.ProcessError(_pageRes.statusCode, "获取到菜品类型列表数据异常");}return tableDataList;}/// <summary>/// 获取餐桌类型列表/// </summary>/// <returns></returns>public ObservableCollection<DiningTypeCheckInfo> GetDingTypeTable(){ObservableCollection<DiningTypeCheckInfo> tableDataList = new ObservableCollection<DiningTypeCheckInfo>(); var _parms = new PageParm { page = DiningCurrentIndex, limit = DiningPageSize, key = DiningTypeKey.Trim() }; var _pageRes = diningTypebll.GetPagesAsync(_parms).Result;//分页查询if (_pageRes.statusCode == (int)ApiEnum.Status){var _pageResData = _pageRes.data;var _tabList = _pageResData.Items;if (_tabList != null&&_tabList.Count!=0){ foreach (var item in _tabList){ var _curTab = new DiningTypeCheckInfo();_curTab.DiningTypeInfo.rtGuid = item.rtGuid;_curTab.DiningTypeInfo.rtName = item.rtName;_curTab.DiningTypeInfo.rtLeastCost = item.rtLeastCost;_curTab.DiningTypeInfo.rtMostNumber = item.rtMostNumber;_curTab.DiningTypeInfo.rtAmount = item.rtAmount;_curTab.DiningTypeInfo.createDate = item.createDate;_curTab.IsCheck = false;tableDataList.Add(_curTab);} DiningTotalNum = (int)_pageResData.TotalItems;DiningCurrentIndex = (int)_pageResData.CurrentPage;DiningTotalIndex = (int)_pageResData.TotalPages;}else{Notice.Show("没有获取到餐桌数据!", "提示", 3, MessageBoxIcon.Info);Logger.Default.ProcessError((int)ApiEnum.Error, "没有获取到餐桌列表数据");}}else{Notice.Show("没有获取到餐桌数据!", "提示", 3, MessageBoxIcon.Info);Logger.Default.ProcessError(_pageRes.statusCode, "获取到餐桌类型列表数据异常");}return tableDataList; }/// <summary>/// 餐桌类型全选命令/// </summary>public ICommand DiningCheckAllCmd{get{return new RelayCommand(o =>{//将业主列表每条记录的isCheck设为与标题状态一致foreach (var record in this.DiningTypeList){record.IsCheck = this.IsCheckAllDining;}});}}/// <summary>/// 菜品类型全选命令/// </summary>public ICommand DishCheckAllCmd{get{return new RelayCommand(o =>{//将业主列表每条记录的isCheck设为与标题状态一致foreach (var record in this.DishTypeList){record.IsCheck = this.IsCheckAllDish;}});}}/// <summary>/// 选择一条餐桌类型数据/// </summary>public ICommand CheckDiningType{get{return new RelayCommand(o =>{if (o != null){var item = o as DiningTypeCheckInfo;item.IsCheck =!item.IsCheck;}});}}/// <summary>/// 选择一条菜品类型数据/// </summary>public ICommand CheckDishType{get{return new RelayCommand(o =>{if (o != null){var item = o as DishTypeCheckInfo;item.IsCheck = !item.IsCheck;}});}}/// <summary>/// 查询按钮的命令处理/// </summary>public ICommand FindCommand{get{return new RelayCommand(o =>{ string typeobj=o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":DiningTypeList = GetDingTypeTable();break;case "dish":DishTypeList = GetDishTypeTable();break;} });}}/// <summary>/// 重置按钮的命令处理/// </summary>public ICommand ResetCommand{get{return new RelayCommand(o =>{string typeobj = o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":DiningCurrentIndex = 1;DiningPageSize = 10;DiningTypeKey = "";DiningTypeList = GetDingTypeTable();break;case "dish":DishCurrentIndex = 1;DishPageSize = 10;DishTypeKey = "";DishTypeList = GetDishTypeTable();break;}});}}/// <summary>/// 新增按钮的命令处理/// </summary>public ICommand AddCommand{get{return new RelayCommand(o =>{string typeobj = o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":WindowX AddWin = new DiningTypeInfo();if (AddWin != null){ AddWin.Closed += new EventHandler(RefreshDiningType);//注册关闭事件AddWin.ShowDialog(); }break;case "dish":DishCurrentIndex = 1;DishPageSize = 10;DishTypeKey = "";DishTypeList = GetDishTypeTable();break;}});}}/// <summary>/// 修改按钮的命令处理/// </summary>public ICommand EditCommand{get{return new RelayCommand(o =>{string typeobj = o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":List<string> delIds = new List<string>();delIds = this.DiningTypeList.Where(r => r.IsCheck == true).Select(r => r.DiningTypeInfo.rtGuid).ToList();if (delIds.Count == 0){MessageBoxX.Show("请选择要编辑的数据.", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations(){MessageBoxIcon = MessageBoxIcon.Warning,ButtonBrush = "#F1C825".ToColor().ToBrush(),});return;}if (delIds.Count >= 2){var _msgRes = MessageBoxX.Show("对不起,一次只能编辑一条数据", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations(){MessageBoxIcon = MessageBoxIcon.Info,ButtonBrush = "#F1C825".ToColor().ToBrush(),});}else{ string selectid = delIds.First();//获取记录idWindowX EditWin = new DiningTypeInfo(selectid); if (EditWin != null){ DiningDishTypeInfoVModel objvmodel = new DiningDishTypeInfoVModel(); DiningType result = diningTypebll.GetModelById(x=>x.rtGuid == selectid).Result.data;objvmodel.DiningTypeItem = result;EditWin.DataContext = objvmodel;EditWin.Closed += new EventHandler(RefreshDiningType);//注册关闭事件EditWin.ShowDialog();} RefreshDiningType(null, null);} break;case "dish":DishTypeList = GetDishTypeTable();break;}});}}/// <summary>/// 删除按钮的命令处理/// </summary>public ICommand DeleteCommand{get{return new RelayCommand(o =>{ string typeobj = o.ToString();//获取命令传递过来的参数,即控件的 CommandParameter参数//处理点击动作(餐桌还是菜品执行不同的处理)switch (typeobj){case "dining":List<string> delIds = new List<string>();delIds = this.DiningTypeList.Where(r => r.IsCheck == true).Select(r => r.DiningTypeInfo.rtGuid).ToList();if (delIds.Count == 0){MessageBoxX.Show("请选择要删除的数据.", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations(){MessageBoxIcon = MessageBoxIcon.Warning,ButtonBrush = "#F1C825".ToColor().ToBrush(),});return;}if (delIds.Count > 0){var _msgRes = MessageBoxX.Show("确定要删除吗?", "提示", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations(){MessageBoxIcon = MessageBoxIcon.Warning,ButtonBrush = "#F1C825".ToColor().ToBrush(),});if (_msgRes == MessageBoxResult.No){return;}var res = new ApiResult<string>() { statusCode = (int)ApiEnum.Error };foreach (string id in delIds){res = diningTypebll.DeleteById(id).Result;} if (res.statusCode == (int)ApiEnum.Status){Notice.Show("成功删除【"+delIds.Count+"】条数据", "提示", 3, MessageBoxIcon.Success);}else{Notice.Show("删除失败!", "提示", 3, MessageBoxIcon.Error);Logger.Default.ProcessError(res.statusCode, "删除餐桌类型失败");}RefreshDiningType(null, null);}break;case "dish":DishTypeList = GetDishTypeTable();break;}});}}/// <summary>/// 刷新餐桌数据/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void RefreshDiningType(object sender, EventArgs e){DiningCurrentIndex = 1;DiningPageSize = 10;DiningTypeKey = "";DiningTypeList = GetDingTypeTable();}}
}
4、运行效果
由每页10条改为每页3条
在wpf中,按钮事件,比如单击事件的响应不是通过onclick这样的方式来实现的,而是通过bind mycommand绑定命令来实现的。
原创不易,打字截图不易,走过路过,不要错过,欢迎点赞,收藏,转载,复制,抄袭,留言,动动你的金手指,早日实现财务自由!