当前位置: 首页 > news >正文

WPFC#超市管理系统(4)入库管理

入库管理

    • 7. 商品入库管理
      • 7.2 入库实现显示名称、图片、单位
      • 7.3 界面设计
      • 7.3 功能实现


7. 商品入库管理

  • 数据库中StockRecord表需要增加商品出入库Type类型为nvarchar(50)
  • C#中的数据库重新同步StockRecord表
  • 在Entity→Model中新建枚举类型StockType
namespace 超市管理系统.Entity.Model
{public enum StockType{入库,出库}
}

7.2 入库实现显示名称、图片、单位

  • 由于StockRecord表内未设置商品名称,因此名称需要通过部分类实现。在Entity→Model中新建StockRecord:BaseModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using 超市管理系统.Entity.Model;
using 超市管理系统.Helper;
using 超市管理系统.ViewModel;namespace 超市管理系统.Entity
{public partial class StockRecord:BaseModel{public string ProductName{get{//ProductId为StockRecord表中的ProductId为StockRecord表中的return ProductViewModel.productProvider.GetAll().FirstOrDefault(t => t.Id == ProductId).Name;}}public BitmapImage BitmapImage{get{string image = ProductViewModel.productProvider.GetAll().FirstOrDefault(t => t.Id == ProductId).Image;return ImageHelper.GetBitmapImage(image);}}}
}

7.3 界面设计

  • 已有UserControl文件InstorageView.xaml,复用ProductView.xaml内容并修改相应内容。
<UserControl x:Class="超市管理系统.View.InstorageView"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:超市管理系统.View" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"mc:Ignorable="d" Background="{Binding AppData.Background}"DataContext="{Binding Source={StaticResource Locator}, Path=InstorageViewModel}"d:DesignHeight="450" d:DesignWidth="800"><i:Interaction.Triggers><i:EventTrigger EventName ="Loaded"><i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/></i:EventTrigger></i:Interaction.Triggers><Grid><Grid.RowDefinitions><RowDefinition Height="40"/><RowDefinition/></Grid.RowDefinitions><Border BorderBrush="#22304B" BorderThickness="0 0 0 1"><TextBlock Text="商品管理" VerticalAlignment="center" Margin="5 0 0 0" Foreground="{Binding AppData.Foreground}" FontSize="16"/></Border><Grid Grid.Row="1"><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition/><RowDefinition Height="auto"/></Grid.RowDefinitions><Grid><StackPanel Orientation="Horizontal" Margin="0 5 0 5"><TextBlock Text="入库管理" VerticalAlignment="Center" Margin="5 0 5 0"/><ComboBox VerticalContentAlignment="Center" MinWidth="100" MaxWidth="200" Margin="5 0 5 0" Height="25"ItemsSource="{Binding ProductList }"SelectedItem="{Binding SelectedProduct}" DisplayMemberPath="Name"/><TextBlock Text="入库数量" VerticalAlignment="Center" Margin="5 0 5 0"/><TextBox Text="{Binding Stock.Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="25" VerticalAlignment="Center"  Width="50" Margin="5 0 5 0"/><Button Content="入库" Command="{Binding SaveCommand}"  Width="80" Margin="0 0 10 0" Height="25"/></StackPanel></Grid><DataGrid Grid.Row="1" ItemsSource="{Binding StockRecordList}"SelectedItem="{Binding SelectedStockRecord}"Style="{StaticResource DataGridStyle}"><DataGrid.Columns><!--普通写法--><!--<DataGridTextColumn Width="auto" Binding="{Binding Id}" Header="序号"/><DataGridTextColumn Width="auto" Binding="{Binding Name}" Header="姓名"/><DataGridTextColumn Width="auto" Binding="{Binding Telephone}" Header="电话"/><DataGridTextColumn Width="auto" Binding="{Binding Address}" Header="地址"/>--><!--数据模板写法--><DataGridTemplateColumn Width="auto" Header="序号"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Id,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="商品编号"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding ProductId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="商品名称"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding ProductName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="商品图片"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><Image Source="{Binding BitmapImage}" ><Image.ToolTip><Grid><Image Source="{Binding BitmapImage}"/></Grid></Image.ToolTip></Image></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="数量"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="单位"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Unit, Mode=OneWay}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="类型"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}" HorizontalAlignment="Left"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="入库日期"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding StockDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}" HorizontalAlignment="Left"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid><Grid Grid.Row="2"><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><StackPanel Grid.Column="0" Margin="0 5 5 5" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center"><TextBlock Text="当前商品:"  Margin="0 0 10 0" Foreground="White" Width="auto" /><TextBlock Text="{Binding SelectedStockRecord.ProductName}" Foreground="White"  Width="auto"/></StackPanel><StackPanel Grid.Column="1"  Margin="0 5 5 5" Orientation="Horizontal" HorizontalAlignment="Right"><Button Content="删除" Command="{Binding DeleteCommand}" Margin="0 0 10 0" Width="80" Height="25"/></StackPanel></Grid></Grid></Grid>
</UserControl>

7.3 功能实现

  • InstorageViewModel,需要设计商品列表和选择项,入库数量绑定StockRecord表中的Quantity。功能有入库和删除两个。
  • 由于商品相关的ViewModel中private ProductProvider productProvider = new ProductProvider()为不同的实例,造成在入库后切换页面Load不会触发更新。因此,将ProductViewModel创建的改为 public static ProductProvider productProvider = new ProductProvider();,其他相关ViewModel删除private ProductProvider productProvider = new ProductProvider(),采用ProductProvider .productProvider 调用。
using CommonServiceLocator;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using 超市管理系统.Entity;
using 超市管理系统.Entity.Model;
using 超市管理系统.View;namespace 超市管理系统.ViewModel
{public class InstorageViewModel :ViewModelBase2{private StockRecordProvider stockRecordProvider = new StockRecordProvider();private List<Product> productList = new List<Product>();public List<Product> ProductList{get { return productList; }set{productList = value;RaisePropertyChanged();}}//当前选中的顾客实体private Product selectedProduct = null;public Product SelectedProduct{get { return selectedProduct; }set{selectedProduct = value;RaisePropertyChanged();}}//选中删除的实体private Product deleteProduct = null;public Product DeleteProduct{get { return deleteProduct; }set{deleteProduct = value;RaisePropertyChanged();}}//新增数量private StockRecord stock;public StockRecord Stock{get { return stock; }set { stock = value; RaisePropertyChanged(); }}//所有入库记录private List<StockRecord> stockRecordList = new List<StockRecord>();public List<StockRecord> StockRecordList{get { return stockRecordList; }set{stockRecordList = value;RaisePropertyChanged();}}//当前选中的入库记录private StockRecord selectedStockRecord = null;public StockRecord SelectedStockRecord{get { return selectedStockRecord; }set{selectedStockRecord = value;RaisePropertyChanged();}}#region commands/// <summary>/// 加载所有供应商/// </summary>public RelayCommand<UserControl> LoadedCommand{get{return new RelayCommand<UserControl>((view) =>{ProductList = ProductViewModel.productProvider.GetAll();StockRecordList = stockRecordProvider.GetAll();Stock = new StockRecord() { Type = StockType.入库.ToString() };});}}public RelayCommand<UserControl> DeleteCommand{get{return new RelayCommand<UserControl>((view) =>{if (SelectedStockRecord != null){var count = stockRecordProvider.Delete(SelectedStockRecord);if (count > 0){//商品数量同步变化//DeleteProduct.Id = (int)SelectedStockRecord.ProductId;//DeleteProduct.Quantity -= (double)SelectedStockRecord.Quantity;//ProductViewModel.productProvider.Update(DeleteProduct);//更新到商品属性中ProductList = ProductViewModel.productProvider.GetAll();MessageBox.Show("删除成功");StockRecordList = stockRecordProvider.GetAll();}}else{ return; }});}}public RelayCommand<UserControl> SaveCommand{get{return new RelayCommand<UserControl>((view) =>{if (SelectedProduct == null) {MessageBox.Show("未选择商品"); return; }if (Stock.Quantity <= 0){MessageBox.Show("入库商品应大于0!"); return;}Stock.ProductId = SelectedProduct.Id;Stock.StockDate = DateTime.Now;int count = stockRecordProvider.Insert(Stock);if (count > 0){//增加商品数量SelectedProduct.Quantity += (double)Stock.Quantity;//更新到商品属性中ProductViewModel.productProvider.Update(selectedProduct);//刷新界面StockRecordList = stockRecordProvider.GetAll();MessageBox.Show("保存成功");//Stock = new StockRecord() { Type = StockType.入库.ToString() };}});}}#endregion}
}

在这里插入图片描述

  • 备注:当前未实现删除入库数据时同步更新商品管理中的数量,等待后续处理。
http://www.dtcms.com/a/311522.html

相关文章:

  • 音视频学习(四十八):PCM和WAV
  • 基于深度学习的医学图像分析:使用GAN实现医学图像增强
  • 进阶向:Python生成艺术图案(分形、数学曲线)
  • MySQL索引解析
  • vue3pinia
  • Corrosion2靶机
  • Cyber Weekly #63
  • 搜索引擎评估革命:用户行为模型如何颠覆传统指标?
  • Sklearn 机器学习 数据聚类 用Numpy自己实现聚类
  • 【C++】类和对象(2)
  • 使用keil点亮stc8核心板的灯
  • 逻辑回归 银行贷款资格判断案列优化 交叉验证,调整阈值,下采样与过采样方法
  • MQTT 入门教程:MQTT工具调式
  • 堆----2.前 K 个高频元素
  • VirtualBox 的 HOST 键(主机键)是 右Ctrl 键(即键盘右侧的 Ctrl 键)笔记250802
  • 学习笔记:无锁队列的原理以及c++实现
  • Linux 高级 I/O 系统调用详解
  • Vue 响应式基础全解析2
  • Node.js中path模块的使用指南
  • InfluxDB 与 Node.js 框架:Express 集成方案(二)
  • 如何在`<link type=“icon“ href=`的`href`中写SVG并使用path标签? 笔记250802
  • 嵌入式 C 语言入门:递归与变量作用域学习笔记 —— 从概念到内存特性
  • 深入 Go 底层原理(十三):interface 的内部表示与动态派发
  • Javaweb————Apache Tomcat服务器介绍及Windows,Linux,MAC三种系统搭建Apache Tomcat
  • 技术文章:覆铜板的阻燃性
  • UniappDay07
  • 【AI】AIService(基本使用与指令定制)
  • cv快速input
  • 【云计算】云主机的亲和性策略(三):云主机 宿主机
  • Springboot原理和Maven高级