C#WPF实战出真汁07--【系统设置】--菜品类型设置

发布于:2025-08-16 ⋅ 阅读:(19) ⋅ 点赞:(0)

1、菜品设置介绍 

菜品设置跟餐桌设置的功能目的是相同的,包括了新增,删除,编辑,分页,查询,重置,全选,全消,列表功能,实现流程也是布局设计,后台逻辑,视图模型绑定,运行测试。

2、布局设计

Grid 是 WPF 中常用的布局控件,以行和列的形式组织子元素,支持灵活的尺寸定义和元素定位。

定义 Grid 的行和列通过 RowDefinitions 和 ColumnDefinitions 属性定义,支持多种尺寸单位:

固定尺寸:Width="100" 或 Height="50"
自动尺寸:Width="Auto"(根据内容调整)
比例尺寸:Width="*" 或 Width="2*"(按比例分配剩余空间)

StackPanel 是一个简单的布局控件,适合子元素的线性排列。通过 Orientation 属性可以控制排列方向(垂直或水平)。可以与其他布局控件嵌套使用以实现复杂布局。对于需要滚动或动态调整大小的场景。StackPanel 是 WPF 中的一个常用布局控件,用于沿水平或垂直方向堆叠子元素。默认情况下,子元素按垂直方向排列,但可以通过设置 Orientation 属性调整为水平排列。StackPanel 不会自动调整子元素的大小,通常用于简单的布局场景。

完整代码

<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:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:HQ.fResApp.UControls"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:pu="clr-namespace:Panuon.UI.Silver;assembly=Panuon.UI.Silver"
    xmlns:vm="clr-namespace:HQ.fResApp.ViewModel"
    Padding="20"
    d:DesignHeight="450"
    d:DesignWidth="1000"
    Background="#f6f9ff"
    mc:Ignorable="d">
    <UserControl.DataContext>
        <vm:DiningDishTypeListVModel />
    </UserControl.DataContext>
    <TabControl
        Margin="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
            Background="Aquamarine"
            BorderBrush="AliceBlue"
            BorderThickness="1"
            Cursor="Hand"
            FontFamily="黑体"
            FontSize="20"
            FontStyle="Normal"
            Header="餐桌类型设置">

            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="12*" />
                    <ColumnDefinition Width="103*" />
                    <ColumnDefinition Width="345*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="55" />
                    <RowDefinition Height="15" />
                    <RowDefinition />
                    <RowDefinition Height="73" />
                </Grid.RowDefinitions>
                <Grid Grid.ColumnSpan="3" Background="#ffffff">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="400*" />
                        <ColumnDefinition Width="61*" />
                        <ColumnDefinition Width="460" />
                    </Grid.ColumnDefinitions>
                    <StackPanel
                        Grid.ColumnSpan="3"
                        Margin="0,0,410,0"
                        Orientation="Horizontal">
                        <TextBox
                            x:Name="keyWhereTab"
                            Width="160"
                            Height="40"
                            Margin="0,15,20,0"
                            pu:TextBoxHelper.Watermark="输入餐桌关键字"
                            FontSize="15"
                            Foreground="#909399"
                            Text="{Binding DiningTypeKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <Button
                            Width="100"
                            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"
                            Command="{Binding FindCommand}"
                            CommandParameter="dining"
                            Content="查询"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff"
                            IsDefault="true" />
                        <Button
                            Width="100"
                            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"
                            Command="{Binding ResetCommand}"
                            CommandParameter="dining"
                            Content="重置"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                    </StackPanel>
                    <StackPanel
                        Grid.Column="2"
                        Margin="80,0,0,0"
                        HorizontalAlignment="Left"
                        Orientation="Horizontal">
                        <Button
                            Width="100"
                            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"
                            Command="{Binding AddCommand}"
                            CommandParameter="dining"
                            Content="新增"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />

                        <Button
                            Width="100"
                            Height="40"
                            Margin="30,12,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/openorder.png"
                            pu:IconHelper.Width="35"
                            Background="Black"
                            BorderBrush="Black"
                            Command="{Binding EditCommand}"
                            CommandParameter="dining"
                            Content="编辑"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                        <Button
                            Width="100"
                            Height="40"
                            Margin="10,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"
                            Command="{Binding DeleteCommand}"
                            CommandParameter="dining"
                            Content="删除"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                    </StackPanel>
                </Grid>
                <Border Grid.Row="1" Grid.ColumnSpan="3" />
                <DataGrid
                    x:Name="dataListTab"
                    Grid.Row="2"
                    Grid.ColumnSpan="3"
                    pu:DataGridHelper.ColumnHorizontalContentAlignment="Center"
                    pu:DataGridHelper.HeaderBackground="#FF009BFF"
                    pu:DataGridHelper.HeaderForeground="#ffffff"
                    pu:DataGridHelper.HeaderMinHeight="50"
                    pu:DataGridHelper.ResizeThumbThickness="0.5"
                    pu:DataGridHelper.SelectedBackground="Transparent"
                    pu:DataGridHelper.SelectedForeground="Red"
                    AutoGenerateColumns="False"
                    CanUserAddRows="False"
                    CanUserDeleteRows="False"
                    CanUserReorderColumns="False"
                    CanUserResizeRows="False"
                    Cursor="Pen"
                    FontSize="16"
                    ItemsSource="{Binding DiningTypeList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    SelectionMode="Extended"
                    SelectionUnit="FullRow">
                    <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow">
                            <EventSetter Event="MouseLeftButtonUp" Handler="DiningItem_GotFocus" />
                            <Setter Property="Height" Value="40" />
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                                <GradientStop Offset="0.98" Color="#FF73BCE8" />
                                                <GradientStop Offset="0" Color="White" />
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>
                    <DataGrid.Columns>
                        <!--  模板列  -->
                        <DataGridTemplateColumn>
                            <!--  模板列的头模板  -->
                            <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox
                                        VerticalAlignment="Center"
                                        pu:CheckBoxHelper.CheckBoxStyle="Switch"
                                        Command="{Binding DataContext.DiningCheckAllCmd, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                        Content="全选"
                                        Foreground="#ffffff"
                                        IsChecked="{Binding DataContext.IsCheckAllDining, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.HeaderTemplate>
                            <!--  模板列的单元格模板  -->
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        Command="{Binding DataContext.CheckDiningType, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                        CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=DataContext}"
                                        IsChecked="{Binding IsCheck, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <!--  此处单元格绑定的属性是视图模型DiningDishTypeListVModel中的DiningTypeInfo属性的各个属性  -->
                        <DataGridTextColumn
                            Width="2*"
                            Binding="{Binding DiningTypeInfo.rtName}"
                            Header="餐桌类型名称"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="2*"
                            Binding="{Binding DiningTypeInfo.rtLeastCost}"
                            Header="最低消费"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="1*"
                            Binding="{Binding DiningTypeInfo.rtMostNumber}"
                            Header="容纳人数"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="1*"
                            Binding="{Binding DiningTypeInfo.rtAmount}"
                            Header="房间个数"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="3*"
                            Binding="{Binding DiningTypeInfo.createDate, ConverterCulture=zh-CN, StringFormat=\{0:yyyy年MM月dd日 dddd HH时mm分\}}"
                            Header="创建日期"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="3*"
                            Binding="{Binding DiningTypeInfo.lastUpDate, ConverterCulture=zh-CN, StringFormat=\{0:yyyy年MM月dd日 dddd HH时mm分\}}"
                            Header="修改日期"
                            IsReadOnly="True" />
                    </DataGrid.Columns>
                </DataGrid>
                <Grid Grid.Row="3" Grid.ColumnSpan="3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="250" />
                    </Grid.ColumnDefinitions>
                    <!--  餐桌类型分页控件  -->
                    <pu:Pagination
                        x:Name="tabPaginationTab"
                        Height="45"
                        Margin="0,0,20,0"
                        HorizontalAlignment="Left"
                        Background="#963F3F3F"
                        CurrentIndex="{Binding DiningCurrentIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        CurrentIndexChanged="tabPaginationTab_CurrentIndexChanged"
                        Cursor="Hand"
                        HoverBrush="#FF009BFF"
                        Spacing="15"
                        TotalIndex="{Binding DiningTotalIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <StackPanel
                        Grid.Column="1"
                        HorizontalAlignment="Right"
                        Orientation="Horizontal">
                        <Button
                            Width="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" />
                        <Button
                            x: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" />
                        <Button
                            Width="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" />
                        <TextBox
                            x:Name="txtPageSizeTab"
                            Grid.Row="2"
                            Width="30"
                            Height="30"
                            HorizontalAlignment="Center"
                            pu:TextBoxHelper.CornerRadius="0"
                            Text="{Binding DiningPageSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <Button
                            Width="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
            Background="Aquamarine"
            BorderBrush="AliceBlue"
            BorderThickness="1"
            Cursor="Hand"
            FontFamily="黑体"
            FontSize="20"
            FontStyle="Normal"
            Header="菜品类型设置">
            <Grid Margin="10">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="32*" />
                    <ColumnDefinition Width="21*" />
                    <ColumnDefinition Width="867*" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="55" />
                    <RowDefinition Height="15" />
                    <RowDefinition />
                    <RowDefinition Height="73" />
                </Grid.RowDefinitions>
                <Grid Grid.ColumnSpan="3" Background="#ffffff">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="460" />
                    </Grid.ColumnDefinitions>
                    <StackPanel
                        Grid.ColumnSpan="2"
                        Margin="0,0,385,0"
                        Orientation="Horizontal">
                        <TextBox
                            x:Name="keyPWhere"
                            Width="160"
                            Height="40"
                            Margin="0,15,20,0"
                            pu:TextBoxHelper.Watermark="输入菜品关键字"
                            FontSize="15"
                            Foreground="#909399"
                            Text="{Binding DishTypeKey, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <Button
                            x:Name="btnPSearch"
                            Width="100"
                            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"
                            Command="{Binding FindCommand}"
                            CommandParameter="dish"
                            Content="查询"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff"
                            IsDefault="true" />
                        <Button
                            Width="100"
                            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/Refresh.png"
                            pu:IconHelper.Width="40"
                            Background="LightSlateGray"
                            BorderBrush="#FF009BFF"
                            Command="{Binding ResetCommand}"
                            CommandParameter="dish"
                            Content="重置"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                    </StackPanel>
                    <StackPanel
                        Grid.Column="1"
                        HorizontalAlignment="Right"
                        Orientation="Horizontal">
                        <Button
                            x:Name="btnPAdd"
                            Width="100"
                            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"
                            Command="{Binding AddCommand}"
                            CommandParameter="dish"
                            Content="新增"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                        <Button
                            Width="100"
                            Height="40"
                            Margin="30,12,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/openorder.png"
                            pu:IconHelper.Width="35"
                            Background="Black"
                            BorderBrush="Black"
                            Command="{Binding EditCommand}"
                            CommandParameter="dish"
                            Content="编辑"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                        <Button
                            x:Name="btnPDel"
                            Width="100"
                            Height="40"
                            Margin="30,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"
                            Command="{Binding DeleteCommand}"
                            CommandParameter="dish"
                            Content="删除"
                            Cursor="Hand"
                            FontSize="16"
                            Foreground="#ffffff" />
                    </StackPanel>
                </Grid>
                <Border Grid.Row="1" Grid.ColumnSpan="3" />
                <DataGrid
                    x:Name="dataPList"
                    Grid.Row="2"
                    Grid.ColumnSpan="3"
                    pu:DataGridHelper.ColumnHorizontalContentAlignment="Center"
                    pu:DataGridHelper.HeaderBackground="#FF009BFF"
                    pu:DataGridHelper.HeaderForeground="#ffffff"
                    pu:DataGridHelper.HeaderMinHeight="50"
                    pu:DataGridHelper.ResizeThumbThickness="0.5"
                    pu:DataGridHelper.SelectedBackground="Transparent"
                    pu:DataGridHelper.SelectedForeground="red"
                    AutoGenerateColumns="False"
                    CanUserAddRows="False"
                    CanUserDeleteRows="False"
                    CanUserReorderColumns="False"
                    CanUserResizeRows="False"
                    Cursor="Pen"
                    FontSize="16"
                    ItemsSource="{Binding DishTypeList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    SelectionMode="Extended"
                    SelectionUnit="FullRow">
                    <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow">
                            <!--  Event绑定MouseLeftButtonUp表示鼠标点击事件,Handler表示具体的事件处理程序  -->
                            <EventSetter Event="MouseLeftButtonUp" Handler="DishItem_GotFocus" />
                            <Setter Property="Height" Value="40" />
                            <!--  样式触发器,IsMouseOver表示鼠标滑过时事件  -->
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                                <GradientStop Offset="0.98" Color="#FF73BCE8" />
                                                <GradientStop Offset="0" Color="White" />
                                            </LinearGradientBrush>
                                        </Setter.Value>
                                    </Setter>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.RowStyle>

                    <DataGrid.Columns>
                        <!--  模板列  -->
                        <DataGridTemplateColumn>
                            <!--  模板列的头模板  -->
                            <DataGridTemplateColumn.HeaderTemplate>
                                <DataTemplate>
                                    <CheckBox
                                        VerticalAlignment="Center"
                                        pu:CheckBoxHelper.CheckBoxStyle="Switch"
                                        Command="{Binding DataContext.DishCheckAllCmd, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                        Content="全选"
                                        Foreground="#ffffff"
                                        IsChecked="{Binding DataContext.IsCheckAllDish, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.HeaderTemplate>
                            <!--  模板列的单元格模板  -->
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox
                                        Name="chk"
                                        HorizontalAlignment="Center"
                                        VerticalAlignment="Center"
                                        Command="{Binding DataContext.CheckDishType, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"
                                        CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=DataContext}"
                                        IsChecked="{Binding IsCheck, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <!--  此处单元格绑定的属性是视图模型DiningDishTypeListVModel中的DishTypeInfo属性的各个属性  -->
                        <DataGridTextColumn
                            Width="3*"
                            Binding="{Binding DishTypeInfo.mtName}"
                            Header="菜品类型名称"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="3*"
                            Binding="{Binding DishTypeInfo.createDate, ConverterCulture=zh-CN, StringFormat=\{0:yyyy年MM月dd日 dddd HH时mm分\}}"
                            Header="创建日期"
                            IsReadOnly="True" />
                        <DataGridTextColumn
                            Width="3*"
                            Binding="{Binding DishTypeInfo.lastUpDate, ConverterCulture=zh-CN, StringFormat=\{0:yyyy年MM月dd日 dddd HH时mm分\}}"
                            Header="修改日期"
                            IsReadOnly="True" />

                    </DataGrid.Columns>
                </DataGrid>
                <Grid Grid.Row="3" Grid.ColumnSpan="3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="250" />
                    </Grid.ColumnDefinitions>
                    <pu:Pagination
                        x:Name="tabPPagination"
                        Height="45"
                        Margin="0,0,20,0"
                        HorizontalAlignment="Left"
                        Background="#963F3F3F"
                        CurrentIndex="{Binding DishCurrentIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        CurrentIndexChanged="ProPagination_CurrentIndexChanged"
                        Cursor="Hand"
                        HoverBrush="#FF009BFF"
                        Spacing="15"
                        TotalIndex="{Binding DishTotalIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                    <StackPanel
                        Grid.Column="1"
                        HorizontalAlignment="Right"
                        Orientation="Horizontal">
                        <Button
                            Width="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" />
                        <Button
                            x:Name="txtPTotalNum"
                            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 DishTotalNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                            FontSize="17"
                            FontWeight="ExtraBold"
                            Foreground="#FF009BFF" />
                        <Button
                            Width="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" />
                        <TextBox
                            x:Name="txtPPageSize"
                            Grid.Row="2"
                            Width="25"
                            Height="30"
                            HorizontalAlignment="Center"
                            pu:TextBoxHelper.CornerRadius="0"
                            Text="{Binding DishPageSize, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                        <Button
                            Width="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>

3、视图模型

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();
            }
        }
        #endregion


        public 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;
                        _curTab.DishTypeInfo.lastUpDate = item.lastUpDate;
                        _curTab.IsCheck = false;
                        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.DiningTypeInfo.lastUpDate = item.lastUpDate;
                        _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 AddDining = new DiningTypeInfo();
                            if (AddDining != null)
                            {
                                AddDining.Closed += new EventHandler(RefreshDiningType);//注册关闭事件
                                AddDining.ShowDialog();
                            }
                            break;
                        case "dish":
                            WindowX AddDish = new DishTypeInfo();
                            if (AddDish != null)
                            {
                                AddDish.Closed += new EventHandler(RefreshDishType);//注册关闭事件
                                AddDish.ShowDialog();
                            }
                            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();//获取记录id
                                WindowX 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();
                                } 
                            }
                            break;
                        case "dish":
                            List<string> dishtypeids = new List<string>();
                            dishtypeids = this.DishTypeList.Where(r => r.IsCheck == true).Select(r => r.DishTypeInfo.mtGuid).ToList();
                            if (dishtypeids.Count == 0)
                            {
                                MessageBoxX.Show("请选择要编辑的数据.", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                                {
                                    MessageBoxIcon = MessageBoxIcon.Warning,
                                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                                });
                                return;
                            }
                            if (dishtypeids.Count >= 2)
                            {
                                var _msgRes = MessageBoxX.Show("对不起,一次只能编辑一条数据", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                                {
                                    MessageBoxIcon = MessageBoxIcon.Info,
                                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                                });
                            }
                            else
                            {
                                string selectid = dishtypeids.First();//获取记录id
                                WindowX EditWin = new DishTypeInfo(selectid);
                                if (EditWin != null)
                                {
                                    DiningDishTypeInfoVModel objvmodel = new DiningDishTypeInfoVModel();
                                    DishType result = dishTypebll.GetModelById(x => x.mtGuid == selectid).Result.data;
                                    objvmodel.DishTypeItem = result;
                                    EditWin.DataContext = objvmodel;
                                    EditWin.Closed += new EventHandler(RefreshDishType);//注册关闭事件
                                    EditWin.ShowDialog();
                                } 
                            }
                            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":
                            List<string> dishids = new List<string>();
                            dishids = this.DishTypeList.Where(r => r.IsCheck == true).Select(r => r.DishTypeInfo.mtGuid).ToList();
                            if (dishids.Count == 0)
                            {
                                MessageBoxX.Show("请选择要删除的数据.", "提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                                {
                                    MessageBoxIcon = MessageBoxIcon.Warning,
                                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                                });
                                return;
                            }
                            if (dishids.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 res2 = new ApiResult<string>() { statusCode = (int)ApiEnum.Error };
                                foreach (string id in dishids)
                                {
                                    res2 = dishTypebll.DeleteById(id).Result;
                                }

                                if (res2.statusCode == (int)ApiEnum.Status)
                                {
                                    Notice.Show("成功删除【" + dishids.Count + "】条数据", "提示", 3, MessageBoxIcon.Success);
                                }
                                else
                                {
                                    Notice.Show("删除失败!", "提示", 3, MessageBoxIcon.Error);
                                    Logger.Default.ProcessError(res2.statusCode, "删除餐桌类型失败");
                                }
                                RefreshDishType(null, null);
                            }
                            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();
        }

        /// <summary>
        /// 刷新菜品数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RefreshDishType(object sender, EventArgs e)
        {
            DishCurrentIndex = 1;
            DishPageSize = 10;
            DishTypeKey = "";
            DishTypeList = GetDishTypeTable();
        }
    }
}

4、新增菜品和编辑菜品

新增和编辑操作时,使用相同的UI布局 

using HQ.COMM;
using HQ.fResApp.BaseModel;
using HQ.MODEL.DBModel;
using Panuon.UI.Silver.Core;
using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using HQ.BLL;
using HQ.fResApp.Utils;
using System.Xml.Linq;
using System.Windows.Controls;
using System.Net.Http.Headers;
using System.Web.Configuration;

namespace HQ.fResApp.ViewModel
{
    public class DiningDishTypeInfoVModel : ViewModelBase
    {
        #region 属性变量
        DiningTypeBLL diningTypeBLL = new DiningTypeBLL();
        DishTypeBLL dishtypeBLL = new DishTypeBLL();


        public string actionType = "新增";//餐桌动作类型
        public string actionType2 = "新增";//菜品动作类型

        WindowX currentwin = null;

        private DiningType diningTypeIItem = new DiningType();
        /// <summary>
        /// 餐桌实体
        /// </summary>
        public DiningType DiningTypeItem
        {
            get { return diningTypeIItem; }
            set
            {
                diningTypeIItem = value;
                OnPropertyChanged();
            }
        }

        private DishType dishTypeItem = new DishType();
        /// <summary>
        /// 餐桌实体
        /// </summary>
        public DishType  DishTypeItem
        {
            get { return dishTypeItem; }
            set
            {
                dishTypeItem = value;
                OnPropertyChanged();
            }
        }
        #endregion

        #region 命令

        /// <summary>
        /// 保存再新增命令处理
        /// </summary>
        public ICommand InsertCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    string opt = o.ToString();//获取命令参数,并根据不同参数判断处理
                    switch (opt)
                    {
                        case "dining"://餐桌
                            DiningInsertHandler();
                            break;
                        case "dish"://菜品
                            DishInsertHandler();
                            break;
                    } 
                });
            }
        }

        /// <summary>
        /// 处理菜品添加程序
        /// </summary>
        private void DishInsertHandler()
        {
            if (DishTypeItem.mtName == null)
            {
                MessageBoxX.Show("***菜品名称不能为空***", "警告提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                {
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });
                return;
            }
            DishTypeItem.mtName = DishTypeItem.mtName.Trim();
            if (DishTypeItem.mtName == "")
            {
                MessageBoxX.Show("***菜品名称不能为空***", "警告提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                {
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });
                return;
            }
            SaveDishType();
        }

      

        /// <summary>
        /// 处理餐桌添加程序 
        /// </summary>
        private void DiningInsertHandler()
        {
            if (DiningTypeItem.rtName == null)
            {
                MessageBoxX.Show("***餐桌名称不能为空***", "警告提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                {
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });
                return;
            }
            DiningTypeItem.rtName = DiningTypeItem.rtName.Trim();
            if (DiningTypeItem.rtName == "")
            {
                MessageBoxX.Show("***餐桌名称不能为空***", "警告提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                {
                    MessageBoxIcon = MessageBoxIcon.Warning,
                    ButtonBrush = "#F1C825".ToColor().ToBrush(),
                });
                return;
            }
            SaveDiningType();
        }




        /// <summary>
        /// 保存并返回命令处理
        /// </summary>
        public ICommand SaveCommand
        {
            get
            {
                return new RelayCommand(o =>
                {
                    currentwin = (WindowX)o;//获取命令传递过来的参数,该参数绑定的就是整个DiningTypeAdd.xaml这个窗体
                    string formname = currentwin.Name;//获取窗体名称
                    switch(formname)
                    {
                        case "diningaddwin":
                            if(DiningTypeItem.rtGuid == null)
                            {
                                actionType = "新增";
                            }
                            else
                            {
                                actionType = "编辑";
                            } 
                            DiningInsertHandler();
                            break;
                        case "dishaddWin":
                            if (DishTypeItem.mtGuid == null)
                            {
                                actionType2 = "新增";
                            }
                            else
                            {
                                actionType2 = "编辑";
                            }
                            DishInsertHandler();
                            break;
                    }
                    if (currentwin != null)
                    {
                        currentwin.Close();
                    }
                    //if (DiningTypeItem.rtName == null)
                    //{
                    //    MessageBoxX.Show("***餐桌名称不能为空***", "警告提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                    //    {
                    //        MessageBoxIcon = MessageBoxIcon.Warning,
                    //        ButtonBrush = "#F1C825".ToColor().ToBrush(),
                    //    });
                    //    return;
                    //}
                    //DiningTypeItem.rtName = DiningTypeItem.rtName.Trim();
                    //if (DiningTypeItem.rtName == "")
                    //{
                    //    MessageBoxX.Show("***餐桌名称不能为空***", "警告提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                    //    {
                    //        MessageBoxIcon = MessageBoxIcon.Warning,
                    //        ButtonBrush = "#F1C825".ToColor().ToBrush(),
                    //    });
                    //    return;
                    //}
                    //if (DiningTypeItem.rtGuid != null)
                    //{
                    //    actionType = "编辑";
                    //}
                    //SaveDiningType();
                    //if (currentwin != null)
                    //{
                    //    currentwin.Close();
                    //}
                });
            }
        }



        #endregion

        #region Function
        /// <summary>
        /// 保存餐桌
        /// </summary>
        /// <returns></returns>
        public void SaveDiningType()
        { 
            //根据不同动作类型处理
            switch (actionType)
            { 
                case "新增":
                    var findobj = diningTypeBLL.IsExistAsync(x => x.rtName == DiningTypeItem.rtName.Trim()).Result;//查询是否存在
                                                                                                                   //新增前必须判断是否重复
                    if (findobj.data.Any)
                    {
                        MessageBoxX.Show("【" + DiningTypeItem.rtName + "】餐桌已存在,请勿重复添加", "失败提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                        {
                            MessageBoxIcon = MessageBoxIcon.Error,
                            ButtonBrush = "#F1C825".ToColor().ToBrush(),
                        });
                        currentwin = null;
                        return;
                    }
                    DiningTypeItem.rtGuid = Guid.NewGuid().ToString();
                    DiningTypeItem.createDate = DateTime.Now;
                    DiningTypeItem.lastUpDate = DateTime.Now;
                    var _addRes = diningTypeBLL.AddAsync(DiningTypeItem).Result;
                    if (_addRes.statusCode == (int)ApiEnum.Status)
                    {
                        Notice.Show("添加成功!", "提示", 3, MessageBoxIcon.Success);
                        DiningTypeItem = new DiningType();//创建新对象的目的在于让页面上控件的值清空
                    }
                    else
                    {
                        Notice.Show("添加失败!", "提示", 3, MessageBoxIcon.Error);
                        Logger.Default.ProcessError(_addRes.statusCode, "添加餐桌类型失败");
                    }
                    break;
                case "编辑":
                    DiningTypeItem.lastUpDate = DateTime.Now;
                    var _addRes2 = diningTypeBLL.UpdateModel(DiningTypeItem).Result;
                    if (_addRes2.statusCode == (int)ApiEnum.Status)
                    {
                        Notice.Show("保存成功!", "提示", 3, MessageBoxIcon.Success); 
                    }
                    else
                    {
                        Notice.Show("保存失败!", "提示", 3, MessageBoxIcon.Error);
                        Logger.Default.ProcessError(_addRes2.statusCode, "添加餐桌类型失败");
                    }
                    break;
            } 
        }


        /// <summary>
        /// 保存菜品
        /// </summary>
        private void SaveDishType()
        {
            //根据不同动作类型处理
            switch (actionType2)
            {
                case "新增":
                    var findobj = dishtypeBLL.IsExistAsync(x => x.mtName == DishTypeItem.mtName.Trim()).Result;//查询是否存在
                    //新增前必须判断是否重复
                    if (findobj.data.Any)
                    {
                        MessageBoxX.Show("【" + DishTypeItem.mtName + "】餐桌已存在,请勿重复添加", "失败提示", Application.Current.MainWindow, MessageBoxButton.OK, new MessageBoxXConfigurations()
                        {
                            MessageBoxIcon = MessageBoxIcon.Error,
                            ButtonBrush = "#F1C825".ToColor().ToBrush(),
                        });
                        currentwin = null;
                        return;
                    }
                    DishTypeItem.mtGuid = Guid.NewGuid().ToString();
                    DishTypeItem.createDate = DateTime.Now;
                    DishTypeItem.lastUpDate = DateTime.Now;
                    var _addRes = dishtypeBLL.AddAsync(DishTypeItem).Result;
                    if (_addRes.statusCode == (int)ApiEnum.Status)
                    {
                        Notice.Show("添加成功!", "提示", 3, MessageBoxIcon.Success);
                        DishTypeItem = new DishType();//创建新对象的目的在于让页面上控件的值清空
                    }
                    else
                    {
                        Notice.Show("添加失败!", "提示", 3, MessageBoxIcon.Error);
                        Logger.Default.ProcessError(_addRes.statusCode, "添加餐桌类型失败");
                    }
                    break;
                case "编辑":
                    DishTypeItem.lastUpDate = DateTime.Now;
                    var _addRes2 = dishtypeBLL.UpdateModel(DishTypeItem).Result;
                    if (_addRes2.statusCode == (int)ApiEnum.Status)
                    {
                        Notice.Show("保存成功!", "提示", 3, MessageBoxIcon.Success);
                    }
                    else
                    {
                        Notice.Show("保存失败!", "提示", 3, MessageBoxIcon.Error);
                        Logger.Default.ProcessError(_addRes2.statusCode, "添加餐桌类型失败");
                    }
                    break;
            }
        }
        #endregion

    }
}

5、运行测试

这个Panuon.UI.Silver做的样式还是蛮漂亮的,加上MaterialDesignThemes和AduSkin搭配,效果是扛把子。

原创不易,打字截图不易,走过路过,不要错过,欢迎点赞,收藏,转载,复制,抄袭,留言,动动你的金手指,早日实现财务自由!


网站公告

今日签到

点亮在社区的每一天
去签到