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綁定命令來實現的。

原創不易,打字截圖不易,走過路過,不要錯過,歡迎點贊,收藏,轉載,復制,抄襲,留言,動動你的金手指,早日實現財務自由!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/919307.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/919307.shtml
英文地址,請注明出處:http://en.pswp.cn/news/919307.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

旋鈕鍵盤項目---foc講解(閉環位置控制)

hello&#xff0c;周六休息了一天&#xff0c;出去打本了。趁著夜色&#xff0c;花費了幾個小時&#xff0c;也是將閉環代碼寫完&#xff0c;參考了燈哥的思路。接下來介紹一下我的整個流程&#xff1a; 一、閉環位置控制思路&#xff1a; 其實懂得了開環&#xff0c;那么閉環…

為什么有些相機“即插即用”,而有些則需要采集卡?

在工業生產中&#xff0c;工業相機是“眼睛”&#xff0c;它幫助我們看到世界&#xff0c;但你知道嗎&#xff1f;不同的工業相機接口就像不同的“通道”&#xff0c;有些“通道”直接就能與計算機連接&#xff0c;而有些則需要一個額外的小配件——圖像采集卡。那么&#xff0…

【計算機網絡 | 第7篇】物理層基本概念

文章目錄物理層基本概念及數據通信系統解析一、物理層的核心定位&#x1f95d;二、物理層的功能&#x1f9fe;三、數據通信系統的模型&#x1f426;?&#x1f525;&#xff08;一&#xff09;源系統&#xff08;二&#xff09;傳輸系統&#xff08;三&#xff09;目的系統四、…

一般情況下,python函數都會返回對象,但有時只調用一個函數,這是在修改這個信息

class Model:def __init__(self):self.training Truedef eval(self):self.training Falsereturn Nonem Model() print(m.training) # True m.eval() # 返回 None print(m.training) # False&#xff0c;模型內部狀態已改變m.eval&#xff08;&#xff09;是在修改m的…

2025-08-17 李沐深度學習17——語義分割

文章目錄1 語義分割1.1 介紹1.2 語義分割應用1.3 實例分割2 轉置卷積2.1 工作原理2.2 為什么叫“轉置”卷積2.3 轉置卷積也是一種卷積3 FCN3.1 核心思想3.2 網絡架構4 樣式遷移4.1 基于 CNN 的樣式遷移4.2 工作流程1 語義分割 1.1 介紹 語義分割&#xff08;Semantic Segment…

《若依》權限控制

若依內置了強大的權限控制系統&#xff0c;為企業級項目提供了通用的解決方案 以CRM系統為例&#xff0c;演示權限功能&#xff08;URL&#xff1a;https://huike-crm.itheima.net) demo賬號&#xff08;超級管理員&#xff09;查看所有功能菜單 zhangsan賬號&#xff08;市…

云原生俱樂部-RH134知識點總結(3)

這個系列的第二篇寫了將近5000字&#xff0c;而且還是刪節內容后的&#xff0c;如RAID就沒寫&#xff0c;因為頭已經很大了。第二篇從早上寫到下午&#xff0c;因為偷懶了&#xff0c;寫著寫著就停筆了。不過好在總算磨完了&#xff0c;現在開始寫RH134系列的最后一篇內容。我這…

股票常見K線

1.底部反彈摸線特點長下影線之后必須有實體陰線踩實之后才考慮。macd綠緩慢收窄過程中的不買&#xff0c;剛轉紅也不買。macd轉紅之后等股價跌回之前macd綠首次收窄的最低點附近&#xff0c;而且跌破了所有均線&#xff0c;可以買入此股票。之后股票一波突破之前平臺震蕩平臺&a…

計算機網絡 THU 考研專欄簡介

本專欄專為清華大學計算機網絡考研復習設計&#xff0c;內容系統全面&#xff0c;涵蓋從基礎概念到重點考點的完整知識體系。具體包括&#xff1a;基礎理論&#xff1a;計算機網絡概念、分類、性能指標及網絡分層模型&#xff08;OSI 七層、TCP/IP 四層&#xff09;。協議與技術…

VSCode打開新的文件夾之后當前打開的文件夾被覆蓋

文件--首選項--設置&#xff1a;搜索showtabs設置為如下&#xff1a;

mac 電腦安裝類似 nvm 的工具,node 版本管理工具

前言 蘋果電腦開發時&#xff0c;有時候需要切換node 版本&#xff0c;window版有nvm可以管理node 版本&#xff0c;mac版本可以用另外一種 //全局安裝n 模塊 sudo npm install n -g//輸入后回車&#xff0c;提示輸入電腦密碼&#xff0c;輸入完密碼回車等待下載完成即可//安裝…

spdlog框架的安裝與使用

spdlog框架的安裝與使用spdlog的安裝spdlog的使用spdlog二次封裝總結&#xff1a;spdlog的安裝 sudo apt-get install libspdlog-devspdlog的使用 同步日志器sync.cc (輸出到顯示器/輸出到指定文件) #include<spdlog/spdlog.h> #include<spdlog/sinks/stdout_color…

使用websockets中的一些問題和解決方法

&#xff08;1&#xff09;TypeError: echo() missing 1 required positional argument: path報錯自己寫的代碼如下&#xff1a;async def echo(websocket, path):...async def main():server await websockets.serve(echo, "0.0.0.0", 666)await server.wait_close…

機器人相關基礎知識

機器人簡介下面給出一份機器人方向“從入門到進階”的極簡知識地圖&#xff0c;按「數學 → 硬件 → 軟件 → 算法 → 應用」五層展開&#xff0c;配合常用開源資源。你可以把它當作“字典”隨時查閱。&#x1f539; 1. 數學層&#xff08;所有算法的地基&#xff09;概念一句話…

Windows Server 打開vGPU RDP HEVC編碼

查看已安裝的驅動[rootlocalhost:~] esxcli software vib list Name Version Vendor Acceptance Level Install Date Platforms ----------------------------- ------------------------------------ ------ -…

OpenAL技術詳解:跨平臺3D音頻API的設計與實踐

引言&#xff1a;OpenAL的定位與價值 OpenAL&#xff08;Open Audio Library&#xff09; 是一套跨平臺的3D音頻應用程序接口&#xff08;API&#xff09;&#xff0c;專為高效渲染多通道三維定位音頻而設計。其API風格與編程范式刻意模仿OpenGL&#xff0c;旨在為游戲開發、虛…

重溫 K8s 基礎概念知識系列五(存儲、配置、安全和策略)

文章目錄一、存儲&#xff08;Storage&#xff09;1.1、Volume1.2、PersistentVolume (PV)1.3、PersistentVolumeClaim (PVC)1.4、StorageClass1.5、PVC 和 PV 的綁定過程&#xff1f;二、配置管理&#xff08;Configuration&#xff09;2.1、ConfigMap2.2、Secret2.3、存活、就…

通過PhotoShop將多張圖片整合為gif動畫

一、準備圖片集合二、導入PS導入PS后點擊確定&#xff1a;導入成功&#xff1a;三、添加時間軸勾選創建幀動畫&#xff1a;此時時間軸進化為幀動畫軸&#xff1a;四、圖片集部署在幀動畫軸點擊幀動畫軸右上角的三道橫杠&#xff0c;從圖層建立幀&#xff1a;此時圖片集已經部署…

Easy Rules 規則引擎詳解

Easy Rules 規則引擎詳解 Easy Rules 是一個輕量級的 Java 規則引擎&#xff0c;它提供了一種簡單而強大的方式來定義和執行業務規則。以下是 Easy Rules 的詳細介紹&#xff1a; 1. 核心概念 1.1 規則 (Rule) 條件 (Condition): 當條件為 true 時執行動作動作 (Action): 條件滿…

優雅設計:打造AI時代的高效后端API接口——領碼課堂深度解析

&#x1f4cc; 摘要 后端API接口已經成為軟件架構的神經系統。微服務演化、AI滲透、自動化治理……這些趨勢迫使我們重新定義接口設計的標準。本文從統一規范、參數校驗、異常處理、性能優化四大維度出發&#xff0c;結合領碼Spark的接口治理平臺與AI賦能實踐&#xff0c;構建一…