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
英文地址,請注明出處:http://en.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 高級攻擊策略配置自定義攻…