超市管理系統 6. 商品管理 6.1 添加商品 6.1 商品管理主界面 6.3 修改商品
6. 商品管理
將前文中的GoodsView全部改成和數據庫一致的ProductView 新增枚舉類型商品類型ProductType.cs
namespace 超市管理系統. Enums
{ public enum ProductType { 水果類, 休閑食品類, 糧油類, 飲料類, 日用品}
}
namespace 超市管理系統. Entity
{ public enum UnitType { 斤, 包, 瓶, 米, 個, 只, 桶}
}
6.1 添加商品
將數據庫Product表的Category從int改為nvarchar(50),在Visual Studio中刪掉Product表并從模型更新新表 新增AddProductView.xaml,復用AddCustomerView.xaml并修改,新增加ImageSource屬性 和上傳圖片的SelectImageCommand 命令。 AddCustomerViewModel內增加SupplierList屬性 、SupplierList屬性 、supplierProvider字段 ,supplierProvider字段 用于添加商品界面初次打開時加載當前現有供應商,并賦值給SupplierList屬性 ,SupplierList屬性 為Combox的當前選擇項。 單價雖然為double類型,但是輸入框中無法輸入小數點,需要將UpdateSourceTrigger設置為LostFocus ,因為PropertyChanged是立即更新,不認小數點。而失去焦點時更新可以有小數點。
< Window x: Class= " 超市管理系統.View.AddProductView" 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: mc= " http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns: local= " clr-namespace:超市管理系統.View" mc: Ignorable= " d" xmlns: i= " http://schemas.microsoft.com/expression/2010/interactivity" WindowStartupLocation = " CenterScreen" DataContext = " {Binding Source={StaticResource Locator}, Path=AddProductViewModel}" Title = " 商品管理" Height = " 450" Width = " 650" > < i: Interaction.Triggers> < i: EventTrigger EventName = " Loaded" > < i: InvokeCommandAction Command = " {Binding LoadedCommand}" CommandParameter = " {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" /> </ i: EventTrigger> </ i: Interaction.Triggers> < Grid> < Grid.RowDefinitions> < RowDefinition Height = " auto" /> < RowDefinition/> < RowDefinition Height = " auto" /> </ Grid.RowDefinitions> < Grid Grid.Row = " 0" Height = " 50" Background = " {Binding AppData.Background}" > < TextBlock Text = " 商品管理" FontSize = " 24" Foreground = " White" VerticalAlignment = " Center" HorizontalAlignment = " Center" /> </ Grid> < Grid Grid.Row = " 1" > < Grid.ColumnDefinitions> < ColumnDefinition/> < ColumnDefinition/> </ Grid.ColumnDefinitions> < StackPanel Grid.Row = " 0" Margin = " 10" HorizontalAlignment = " Center" > < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 供應商:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < ComboBox ItemsSource = " {Binding SupplierList}" SelectedItem = " {Binding Supplier}" DisplayMemberPath = " Name" MinWidth = " 200" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 單 位:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < ComboBox ItemsSource = " {Binding Product.Units}" SelectedItem = " {Binding Product.Unit}" Width = " 200" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 類 型:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < ComboBox ItemsSource = " {Binding Product.ProductType}" SelectedItem = " {Binding Product.Category}" Width = " 200" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 商品名:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < TextBox Text = " {Binding Product.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 200" Height = " 30" VerticalAlignment = " Center" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 單 價:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < TextBox Text = " {Binding Product.Price, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Width = " 200" Height = " 30" VerticalAlignment = " Center" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 庫 存:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < TextBox Text = " {Binding Product.Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 200" /> </ StackPanel> </ StackPanel> < Grid Grid.Column = " 1" Margin = " 10" Background = " #E2E2E2" > < TextBlock Text = " 選擇商品圖片" HorizontalAlignment = " Center" VerticalAlignment = " Center" /> < Border Background = " Transparent" > < i: Interaction.Triggers> < i: EventTrigger EventName = " MouseUp" > < i: InvokeCommandAction Command = " {Binding SelectImageCommand}" /> </ i: EventTrigger> </ i: Interaction.Triggers> < Image Source = " {Binding ImageSource}" > </ Image> </ Border> </ Grid> </ Grid> < StackPanel Grid.Row = " 2" Margin = " 10" Orientation = " Horizontal" HorizontalAlignment = " Right" > < Button x: Name= " button1" Content = " 新增" Command = " {Binding AddCommand}" CommandParameter = " {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Margin = " 10" Width = " 60" Height = " 25" /> < Button x: Name= " button2" Content = " 關閉" Command = " {Binding ExitCommand}" CommandParameter = " {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Margin = " 10" Width = " 60" Height = " 25" /> </ StackPanel> </ Grid>
</ Window>
AddCustomerViewModel.cs包含屬性由下拉框的供應商列表、單位、類型、商品名、單價、庫存,以及當前供應商等內容,實現代碼如下: 注意需要為新增按鈕增加Product.SupplierId = Supplier.Id 將當前供應商Id傳入
using GalaSoft. MvvmLight. Command ;
using Microsoft. Win32 ;
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
using System. Windows ;
using System. Windows. Media. Imaging ;
using 超市管理系統. Entity;
using 超市管理系統. Helper; namespace 超市管理系統. ViewModel
{ public class AddProductViewModel : ViewModelBase2 { private ProductProvider productProvider = new ProductProvider ( ) ; private Product product; public Product Product{ get { return product; } set { product = value ; RaisePropertyChanged ( ) ; } } private SupplierProvider supplierProvider = new SupplierProvider ( ) ; private List< Supplier> supplierList = new List< Supplier> ( ) ; public List< Supplier> SupplierList{ get { return supplierList; } set { supplierList = value ; RaisePropertyChanged ( ) ; } } private Supplier supplier; public Supplier Supplier{ get { return supplier; } set { supplier = value ; RaisePropertyChanged ( ) ; } } #region commands public RelayCommand< Window> LoadedCommand{ get { return new RelayCommand< Window> ( ( view) => { Product = new Product ( ) ; SupplierList = supplierProvider. GetAll ( ) ; ImageSource = null ; Supplier = null ; } ) ; } } public RelayCommand< Window> AddCommand{ get { return new RelayCommand< Window> ( ( view) => { if ( string . IsNullOrEmpty ( Product. Name) ) { MessageBox. Show ( "姓名不能為空!" ) ; return ; } Product. SupplierId = Supplier. Id; Product. InsertDate = DateTime. Now; int count = productProvider. Insert ( Product) ; if ( count > 0 ) { MessageBox. Show ( "操作成功!" ) ; } view. DialogResult = true ; view. Close ( ) ; } ) ; } } private BitmapImage imageSourece; public BitmapImage ImageSource{ get { return imageSourece; } set { imageSourece = value ; RaisePropertyChanged ( ) ; } } public RelayCommand< Window> SelectImageCommand{ get { return new RelayCommand< Window> ( ( view) => { OpenFileDialog openFileDialog = new OpenFileDialog ( ) ; openFileDialog. Title = "選擇圖片" ; openFileDialog. Filter = "圖片文件(*.jpg)|*.jpg|所有文件(*.*)|*.*" ; openFileDialog. FilterIndex = 1 ; openFileDialog. Multiselect = false ; openFileDialog. RestoreDirectory = true ; if ( openFileDialog. ShowDialog ( ) . Value == true ) { string fileName = openFileDialog. FileName; ImageSource = new BitmapImage ( new Uri ( fileName) ) ; product. Image = ImageHelper. GetImageString ( fileName) ; var s = ImageHelper. GetBitmapImage ( product. Image) ; ImageSource = s; } } ) ; } } public RelayCommand< Window> ExitCommand{ get { return new RelayCommand< Window> ( ( view) => { Product = new Product ( ) ; } ) ; } } #endregion }
}
新增商品時添加的圖片需要將圖片轉換為二進制流。在項目新建Helper文件夾,增加ImageHelper類包含函數GetImageString獲取圖片二進制流和GetBitmapImage二進制流轉化為圖片 。
using System ;
using System. Collections. Generic ;
using System. IO ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
using System. Windows ;
using System. Windows. Media. Imaging ; namespace 超市管理系統. Helper
{ public class ImageHelper { public static string GetImageString ( string fileName) { try { FileStream fileStream = new FileStream ( fileName, FileMode. Open, FileAccess. Read) ; byte [ ] buffer = new byte [ fileStream. Length] ; fileStream. Read ( buffer, 0 , buffer. Length) ; return Convert. ToBase64String ( buffer) ; } catch ( Exception e) { MessageBox. Show ( e. Message) ; return string . Empty; } } public static BitmapImage GetBitmapImage ( string _buffer) { BitmapImage image = new BitmapImage ( ) ; try { byte [ ] buffer = Convert. FromBase64String ( _buffer) ; MemoryStream stream = new MemoryStream ( buffer, 0 , buffer. Length) ; stream. Write ( buffer, 0 , buffer. Length) ; stream. Position = 0 ; image. BeginInit ( ) ; image. CacheOption = BitmapCacheOption. OnLoad; image. StreamSource = stream; image. EndInit ( ) ; image. Freeze ( ) ; return image; } catch ( Exception e) { MessageBox. Show ( e. Message) ; return image; } } }
}
6.1 商品管理主界面
ProductView.xaml內容復用CustomerView.xaml并將Customer修改為Product,將綁定的屬性改為Product屬性 通過Image.ToolTip 將鼠標懸停可實現圖片放大
< UserControl x: Class= " 超市管理系統.View.ProductView" 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=ProductViewModel}" 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/> < RowDefinition Height = " auto" /> </ Grid.RowDefinitions> < DataGrid ItemsSource = " {Binding ProductList}" SelectedItem = " {Binding SelectedProduct}" 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 Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> </ 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 Price, 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 Unit, 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 Category, 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 Quantity, 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 InsertDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style = " { StaticResource DataGridTextBoxStyle} " HorizontalAlignment = " Left" /> </ Grid> </ DataTemplate> </ DataGridTemplateColumn.CellTemplate> </ DataGridTemplateColumn> </ DataGrid.Columns> </ DataGrid> < Grid Grid.Row = " 1" > < 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 SelectedProduct.Name}" Foreground = " White" Width = " auto" /> </ StackPanel> < StackPanel Grid.Column = " 1" Margin = " 0 5 5 5" Orientation = " Horizontal" HorizontalAlignment = " Right" > < Button Content = " 新增商品" Command = " {Binding OpenAddViewCommand}" Margin = " 0 0 10 0" Width = " 80" Height = " 25" /> < Button Content = " 刪除商品" Command = " {Binding DeleteCommand}" Margin = " 0 0 10 0" Width = " 80" Height = " 25" /> < Button Content = " 修改" Command = " {Binding EditCommand}" Width = " 80" Margin = " 0 0 10 0" Height = " 25" /> < Button Content = " 保存" Command = " {Binding SaveCommand}" Width = " 80" Margin = " 0 0 10 0" Height = " 25" /> </ StackPanel> </ Grid> </ Grid> </ Grid>
</ UserControl>
ProductViewModel.cs內實現代碼如下:
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 超市管理系統. View; namespace 超市管理系統. ViewModel
{ public class ProductViewModel : ViewModelBase2 { private ProductProvider productProvider = new ProductProvider ( ) ; private List< Product> productList = new List< Product> ( ) ; public List< Product> ProductList{ get { return productList; } set { productList = value ; RaisePropertyChanged ( ) ; } } private Product selectedProduct; public Product SelectedProduct{ get { return selectedProduct; } set { selectedProduct = value ; RaisePropertyChanged ( ) ; } } #region commands public RelayCommand< UserControl> LoadedCommand{ get { return new RelayCommand< UserControl> ( ( view) => { ProductList = productProvider. GetAll ( ) ; } ) ; } } public RelayCommand< UserControl> OpenAddViewCommand{ get { return new RelayCommand< UserControl> ( ( view) => { AddProductView addProductView = new AddProductView ( ) ; if ( addProductView. ShowDialog ( ) . Value == true ) { ProductList = productProvider. GetAll ( ) ; } } ) ; } } public RelayCommand< UserControl> DeleteCommand{ get { return new RelayCommand< UserControl> ( ( view) => { if ( SelectedProduct == null ) { return ; } if ( Dialog. Show ( ) == true ) { var count = productProvider. Delete ( SelectedProduct) ; if ( count > 0 ) { MessageBox. Show ( "刪除成功" ) ; ProductList = productProvider. GetAll ( ) ; } } } ) ; } } public RelayCommand< UserControl> SaveCommand{ get { return new RelayCommand< UserControl> ( ( view) => { var count = productProvider. Save ( ) ; if ( count > 0 ) { MessageBox. Show ( "保存成功" ) ; ProductList = productProvider. GetAll ( ) ; } } ) ; } } public RelayCommand< Window> EditCommand{ get { return new RelayCommand< Window> ( ( view) => { if ( SelectedProduct == null ) { return ; } var vm = ServiceLocator. Current. GetInstance < EditProductViewModel> ( ) ; vm. Product = SelectedProduct; EditProductView editProductView = new EditProductView ( ) ; if ( editProductView. ShowDialog ( ) . Value == true ) { ProductList = productProvider. GetAll ( ) ; } } ) ; } } #endregion }
}
6.3 修改商品
新增EditProductView.xaml,復用EditCustomerView.xaml并修改,包含屬性由下拉框的供應商列表、單位、類型、商品名、單價、庫存,以及當前供應商等內容。
< Window x: Class= " 超市管理系統.View.EditProductView" 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: mc= " http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns: local= " clr-namespace:超市管理系統.View" xmlns: i= " http://schemas.microsoft.com/expression/2010/interactivity" mc: Ignorable= " d" WindowStartupLocation = " CenterScreen" DataContext = " {Binding Source={StaticResource Locator}, Path=EditProductViewModel}" Title = " 修改商品" Height = " 450" Width = " 650" > < i: Interaction.Triggers> < i: EventTrigger EventName = " Loaded" > < i: InvokeCommandAction Command = " {Binding LoadedCommand}" CommandParameter = " {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" /> </ i: EventTrigger> </ i: Interaction.Triggers> < Grid> < Grid.RowDefinitions> < RowDefinition Height = " auto" /> < RowDefinition/> < RowDefinition Height = " auto" /> </ Grid.RowDefinitions> < Grid Grid.Row = " 0" Height = " 50" Background = " {Binding AppData.Background}" > < TextBlock Text = " 修改商品" FontSize = " 24" Foreground = " White" VerticalAlignment = " Center" HorizontalAlignment = " Center" /> </ Grid> < Grid Grid.Row = " 1" > < Grid.ColumnDefinitions> < ColumnDefinition/> < ColumnDefinition/> </ Grid.ColumnDefinitions> < StackPanel Grid.Row = " 0" Margin = " 10" HorizontalAlignment = " Center" > < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 供應商:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < ComboBox ItemsSource = " {Binding SupplierList}" SelectedItem = " {Binding Supplier}" DisplayMemberPath = " Name" MinWidth = " 200" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 單 位:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < ComboBox ItemsSource = " {Binding Product.Units}" SelectedItem = " {Binding Product.Unit}" Width = " 200" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 類 型:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < ComboBox ItemsSource = " {Binding Product.ProductType}" SelectedItem = " {Binding Product.Category}" Width = " 200" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 商品名:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < TextBox Text = " {Binding Product.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 200" Height = " 30" VerticalAlignment = " Center" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 單 價:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < TextBox Text = " {Binding Product.Price, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 200" Height = " 30" VerticalAlignment = " Center" /> </ StackPanel> < StackPanel Orientation = " Horizontal" Height = " 30" Margin = " 0 5 0 10" > < TextBlock Text = " 庫 存:" Width = " 70" FontSize = " 18" VerticalAlignment = " Center" /> < TextBox Text = " {Binding Product.Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 200" /> </ StackPanel> </ StackPanel> < Grid Grid.Column = " 1" Margin = " 10" Background = " #E2E2E2" > < TextBlock Text = " 選擇商品圖片" HorizontalAlignment = " Center" VerticalAlignment = " Center" /> < Border Background = " Transparent" > < i: Interaction.Triggers> < i: EventTrigger EventName = " MouseUp" > < i: InvokeCommandAction Command = " {Binding SelectImageCommand}" /> </ i: EventTrigger> </ i: Interaction.Triggers> < Image Source = " {Binding ImageSource}" > </ Image> </ Border> </ Grid> </ Grid> < StackPanel Grid.Row = " 2" Margin = " 10" Orientation = " Horizontal" HorizontalAlignment = " Right" > < Button x: Name= " button1" Content = " 確定" Command = " {Binding OKCommand}" CommandParameter = " {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Margin = " 10" Width = " 60" Height = " 25" /> < Button x: Name= " button2" Content = " 關閉" Command = " {Binding ExitCommand}" CommandParameter = " {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Margin = " 10" Width = " 60" Height = " 25" /> </ StackPanel> </ Grid>
</ Window>
EditProductViewModel.cs復用 AddProductViewModel.cs內的代碼與命令。在OK按鈕的命令中需要Product.SupplierId = Supplier.Id; 將當前供應商Id傳入
using GalaSoft. MvvmLight. Command ;
using Microsoft. Win32 ;
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
using System. Windows ;
using System. Windows. Media ;
using System. Windows. Media. Imaging ;
using 超市管理系統. Entity;
using 超市管理系統. Helper; namespace 超市管理系統. ViewModel
{ public class EditProductViewModel : ViewModelBase2 { private ProductProvider productProvider = new ProductProvider ( ) ; private Product product; public Product Product{ get { return product; } set { product = value ; RaisePropertyChanged ( ) ; } } private SupplierProvider supplierProvider = new SupplierProvider ( ) ; private List< Supplier> supplierList = new List< Supplier> ( ) ; public List< Supplier> SupplierList{ get { return supplierList; } set { supplierList = value ; RaisePropertyChanged ( ) ; } } private Supplier supplier; public Supplier Supplier{ get { return supplier; } set { supplier = value ; RaisePropertyChanged ( ) ; } } private BitmapImage imageSourece; public BitmapImage ImageSource{ get { return imageSourece; } set { imageSourece = value ; RaisePropertyChanged ( ) ; } } #region commands public RelayCommand< Window> LoadedCommand{ get { return new RelayCommand< Window> ( ( view) => { ImageSource = Product. BitmapImage; SupplierList = supplierProvider. GetAll ( ) ; Supplier = SupplierList. FirstOrDefault ( t => t. Id == Product. SupplierId) ; } ) ; } } public RelayCommand< Window> SelectImageCommand{ get { return new RelayCommand< Window> ( ( view) => { OpenFileDialog openFileDialog = new OpenFileDialog ( ) ; openFileDialog. Title = "選擇圖片" ; openFileDialog. Filter = "圖片文件(*.jpg)|*.jpg|所有文件(*.*)|*.*" ; openFileDialog. FilterIndex = 1 ; openFileDialog. Multiselect = false ; openFileDialog. RestoreDirectory = true ; if ( openFileDialog. ShowDialog ( ) . Value == true ) { string fileName = openFileDialog. FileName; ImageSource = new BitmapImage ( new Uri ( fileName) ) ; product. Image = ImageHelper. GetImageString ( fileName) ; } } ) ; } } public RelayCommand< Window> OKCommand{ get { return new RelayCommand< Window> ( ( view) => { if ( string . IsNullOrEmpty ( Product. Name) ) { MessageBox. Show ( "姓名不能為空!" ) ; return ; } Product. SupplierId = Supplier. Id; int count = productProvider. Update ( Product) ; if ( count > 0 ) { MessageBox. Show ( "操作成功!" ) ; } view. DialogResult = true ; view. Close ( ) ; } ) ; } } public RelayCommand< Window> ExitCommand{ get { return new RelayCommand< Window> ( ( view) => { Product = new Product ( ) ; } ) ; } } #endregion }
}