入庫管理 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 { 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> < 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 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 ) { 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 }
}
備注:當前未實現刪除入庫數據時同步更新商品管理中的數量,等待后續處理。