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綁定命令來實現的。
原創不易,打字截圖不易,走過路過,不要錯過,歡迎點贊,收藏,轉載,復制,抄襲,留言,動動你的金手指,早日實現財務自由!