-
- 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 }}
