WPFC#超市管理系统(3)商品管理

超市管理系统

    • 6. 商品管理
      • 6.1 添加商品
      • 6.1 商品管理主界面
      • 6.3 修改商品


6. 商品管理

  • 将前文中的GoodsView全部改成和数据库一致的ProductView
  • 新增枚举类型商品类型ProductType.cs
namespace 超市管理系统.Enums
{public enum ProductType{水果类,休闲食品类,粮油类,饮料类,日用品}
}
  • 新增枚举类型商品单位UnitType.cs
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设置height时,VerticalAlignment不生效,此地设置给grid--><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 commandspublic 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;//将当前供应商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{/// <summary>/// 获取图片的二进制流/// </summary>/// <param name="fileName"></param>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);//从fileStream中读取数据,写入buffer中return Convert.ToBase64String(buffer);}catch (Exception e){MessageBox.Show(e.Message);return string.Empty;}}/// <summary>/// 二进制流转换成图片资源/// </summary>/// <param name="fileName"></param>/// <returns></returns>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><!--普通写法--><!--<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 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/// <summary>/// 加载所有供应商/// </summary>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设置height时,VerticalAlignment不生效,此地设置给grid--><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 commandspublic 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);//测试转换是否正常//var s = ImageHelper.GetBitmapImage(product.Image);//ImageSource = s;}});}}public RelayCommand<Window> OKCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Product.Name)){MessageBox.Show("姓名不能为空!");return;}Product.SupplierId = Supplier.Id;//将当前供应商Id传入//Product.InsertDate = DateTime.Now;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}
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/93591.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/93591.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

openwrt中br-lan,eth0,eth0.1,eth0.2

CPU是QCA9558 有两个以太网接口 这个好像没有外接交换机直接印出来的 openwrt中br-lan,eth0,eth0.1,eth0.2 https://blog.csdn.net/f2157120/article/details/119460852 这个哥用的是 链接: DomyWifi DW33D 路由器 CPU是QCA9558 有两个以太网接口 因为CPU没集成千兆交换&…

RAG实战指南 Day 29:RAG系统成本控制与规模化

【RAG实战指南 Day 29】RAG系统成本控制与规模化 开篇 欢迎来到"RAG实战指南"系列的第29天&#xff01;今天我们将深入探讨RAG系统的成本控制与规模化部署策略。当RAG系统从原型阶段进入生产环境时&#xff0c;如何经济高效地扩展系统规模、控制运营成本成为关键挑…

React 中获取当前路由信息

在 React 中获取当前路由信息&#xff0c;根据使用的路由库不同&#xff08;如 React Router v5/v6 或 Next.js&#xff09;&#xff0c;方法也有所区别。以下是常见场景的解决方案&#xff1a;1. 使用 React Router v6 获取当前路径&#xff08;pathname&#xff09;、查询参数…

Sklearn 机器学习 随机森林 网格搜索获取最优参数

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Sklearn 机器学习:随机森林 + 网格搜索获取最优参数实战指南 在构建机器学习模型时,…

力扣-101.对称二叉树

题目链接 101.对称二叉树 class Solution {public boolean check(TreeNode l, TreeNode r) {if (l null && r null)return true;if ((l null && r ! null) || (r null && l ! null))return false;if (l.val ! r.val)return false;return check(l…

从句--02-1--done,doing ,prep 做定语

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录定语1.done&#xff08;过去分词&#xff09;做定语一、过去分词作定语的位置二、过去分词作定语的语义特点三、过去分词作定语与现在分词作定语的区别四、过去分词作…

JVM全面解析

摘要&#xff1a;JVM是Java程序运行的核心环境&#xff0c;负责解释执行字节码并管理内存。其核心功能包括类加载与验证、字节码执行优化、内存管理与垃圾回收&#xff08;GC&#xff09;、跨平台支持及安全性保障。JVM架构包含程序计数器、虚拟机栈、本地方法栈、堆和方法区等…

SDC命令详解:使用write_script命令进行输出

相关阅读 SDC输出命令https://blog.csdn.net/weixin_45791458/category_12993272.html?spm1001.2014.3001.5482 write_script命令用于将设计中的属性设置命令输出为脚本文件&#xff08;其实它并不是一个SDC命令&#xff0c;归为此类只是为了方便管理&#xff09;&#xff0c…

‌CASE WHEN THEN ELSE END‌

‌CASE WHEN THEN ELSE END‌ 是SQL中实现条件逻辑的核心表达式&#xff0c;支持单字段匹配和多条件判断&#xff0c;适用于数据处理、分类统计等场景。‌基本语法形式‌SQL中CASE表达式有两种标准形式&#xff1a;1‌ 简单CASE表达式‌&#xff08;字段直接匹配&#xff09;C…

飞单诱因:管理漏洞与人性交织

飞单看似是 “员工个人行为”&#xff0c;实则是餐厅管理、激励机制、外部环境等多重因素共同作用的结果。要根治飞单&#xff0c;需先理清背后的 “动力源”—— 员工为何选择冒险&#xff1f;一、“收入失衡”&#xff1a;薪资与付出不匹配的 “补偿心理”基层员工&#xff0…

工作笔记-----FreeRTOS中的lwIP网络任务为什么会让出CPU

工作笔记-----FreeRTOS中的lwIP网络任务为什么会让出CPU Author: 明月清了个风Date&#xff1a; 2025.7.30Ps:最近接触了在FreeRTOS中使用lwIP实现的网络任务&#xff0c;但是在看项目代码的过程中出现了一些疑问——网络任务的优先级为所有任务中最高的&#xff0c;并且任务框…

在 CentOS 系统上安装 Docker

在 CentOS 系统上安装 Docker&#xff0c;可按以下步骤操作&#xff1a;一、卸载旧版本&#xff08;如存在&#xff09;bashsudo yum remove docker \docker-client \docker-client-latest \docker-common \docker-latest \docker-latest-logrotate \docker-logrotate \docker-…

【CVPR2025】FlowRAM:用区域感知与流匹配加速高精度机器人操作策略学习

文章目录FlowRAM&#xff1a;用区域感知与流匹配加速高精度机器人操作策略学习一、问题出在哪里&#xff1f;方法部分&#xff1a;从结构到机制&#xff0c;详解 FlowRAM 的内部设计逻辑1. 动态半径调度器&#xff1a;自适应注意力机制在 3D 感知中的实现2. 多模态编码器与序列…

图片查重从设计到实现(5)Milvus可视化工具

要通过网页&#xff08;Web&#xff09;访问和管理 Milvus 向量数据库&#xff0c;可以使用官方提供的 Milvus Web UI 工具&#xff0c;这是一款可视化管理界面&#xff0c;支持查看集合、向量数据、执行基本操作等功能。以下是具体的部署和访问方法&#xff1a; 一、部署 Milv…

Linux-awk与sed

文章目录一、AWK1. awk 是什么&#xff1f;2. awk 的基础语法2.1 选项2.2 模式2.3 动作3. awk 的内置变量4. 典型应用场景及示例4.1 打印特定列4.2 条件筛选4.3 使用正则表达式4.4 统计行数4.5 字段操作4.6 使用内置函数4.7 多文件处理4.8 使用自定义变量5. 高级应用&#xff1…

文件加密工具(勒索病毒加密方式)

语言&#xff1a;C# WPF功能&#xff1a;文件加/解密本程序不提供下载&#xff0c;该程序新手操作不当&#xff0c;可能会导致文件加密后无法解密问题&#xff0c;解密需要独立私钥private.key文件支持&#xff0c;没有私钥加密文件是无法被解密的。更新&#xff1a;2025年7月3…

IOC实现原理源码解析

Spring三级缓存流程图singletonObjects&#xff08;一级缓存&#xff09;&#xff1a;缓存经过了完整生命周期的Bean&#xff1b;arlySingletonobjects&#xff08;二级缓存&#xff09;&#xff1a;缓存未经过完整生命周期的Bean&#xff0c;如果某个Bean出现了循环依赖&#…

笔记本电脑磁盘维护指南:WIN11系统磁盘维护完全手册

1. 引言 在当今数字化时代,笔记本电脑已经成为我们工作、学习和娱乐不可或缺的重要工具。随着Windows 11操作系统的普及和应用,用户对于系统性能和稳定性的要求越来越高。然而,许多用户往往忽视了一个至关重要的方面——磁盘维护。磁盘作为计算机系统中负责数据存储和读取的…

李宏毅2025《机器学习》-第九讲:大型语言模型评测的困境与“古德哈特定律”**

摘要&#xff1a; 随着大型语言模型&#xff08;LLM&#xff09;的推理能力日益增强&#xff0c;如何公平、准确地评测其“智力”水平&#xff0c;成了一个极其棘手的问题。本文基于李宏毅教授的最新课程&#xff0c;深入探讨了当前LLM评测面临的困境。文章首先揭示了标准数学和…

Spring Boot集成Chaos Monkey:构建高韧性系统的故障注入实战指南

Spring Boot集成Chaos Monkey&#xff1a;构建高韧性系统的故障注入实战指南一、Chaos Engineering核心原理1.1 混沌工程价值矩阵1.2 Chaos Monkey核心攻击类型二、Spring Boot集成Chaos Monkey2.1 基础集成配置依赖引入配置文件 - application.yml2.2 高级攻击策略配置自定义攻…