WPFC#超市管理系統(2)顧客管理、供應商管理、用戶管理

超市管理系統

    • 3. 顧客管理
      • 3.1 顧客新增
      • 3.2 DataGrid樣式
      • 3.3 顧客刪除
      • 3.4 顧客修改
    • 4. 供應商管理
      • 4.1 供應商管理主界面
      • 4.2 新增供應商
      • 4.3 修改供應商
    • 5. 用戶管理
      • 5.1 用戶管理主界面
      • 5.2 新增用戶
      • 5.3 修改用戶
  • 總結

3. 顧客管理

  • 在CustomerView.xaml使用命令綁定方式添加頁面加載Loaded事件的觸發器,其公共模板如下,應用于各菜單頁面
    <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>
  • 在CustomerViewModel.cs添加Loaded事件,其公共模板如下,同時應用于各菜單頁面
 public RelayCommand<UserControl> LoadedCommand{get{return new RelayCommand<UserControl>((view) =>{});}}

3.1 顧客新增

  • 在View文件夾新增窗體AddCustomerView.xaml
<Window x:Class="超市管理系統.View.AddCustomerView"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"DataContext="{Binding Source={StaticResource Locator}, Path=AddCustomerViewModel}"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><StackPanel Grid.Row="1" Margin="10" HorizontalAlignment="Center" Width="500"><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="姓名:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Customer.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="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Customer.Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="地址:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Customer.Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" Height="30" VerticalAlignment="Center"/></StackPanel></StackPanel><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>
  • ViewModel文件夾新建AddCustomerViewModel類并繼承ViewModelBase2,按照格式放在容器ViewModelLocator中,將AddCustomerView.xaml的DataContext設置綁定到AddCustomerViewModel上,功能實現代碼如下:
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;namespace 超市管理系統.ViewModel
{public class AddCustomerViewModel:ViewModelBase2{private CustomerProvider customerProvider = new CustomerProvider();private Customer customer;public Customer Customer{get { return customer; }set{ customer = value;RaisePropertyChanged();}}public RelayCommand<Window> LoadedCommand{get{return new RelayCommand<Window>((view) =>{Customer = new Customer();});}}public RelayCommand<Window> AddCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Customer.Name)){MessageBox.Show("姓名不能為空!");return;}if (string.IsNullOrEmpty(Customer.Telephone)){MessageBox.Show("地址不能為空!");return;}if (string.IsNullOrEmpty(Customer.Address)){MessageBox.Show("電話不能為空!");return;}Customer.InsertDate = DateTime.Now;int count = customerProvider.Insert(Customer);if (count > 0) {MessageBox.Show("操作成功!");}view.DialogResult = true;view.Close();});}}public RelayCommand<Window> ExitCommand{get{return new RelayCommand<Window>((view) =>{Customer = new Customer();});}}}
}

在這里插入圖片描述

3.2 DataGrid樣式

  • 在Style新增字源詞典DataGrid.xaml,設置顧客管理界面的樣式,添加進App.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><!--DataGrid主樣式--><Style x:Key="DataGridStyle" TargetType="DataGrid"><Setter Property="Focusable" Value="False"/><Setter Property="SelectionMode" Value="Extended"/><Setter Property="CanUserAddRows" Value="False"/><Setter Property="CanUserDeleteRows" Value="False"/><Setter Property="AutoGenerateColumns" Value="False"/></Style><!--標題樣式--><Style TargetType="DataGridColumnHeader"><Setter Property="HorizontalContentAlignment" Value="Center"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="DataGridColumnHeader"><Border BorderBrush="#DBDDDF" Background="#ECECEC" Padding="3" MinHeight="35" BorderThickness="0 0 1 1"><ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"VerticalAlignment="{TemplateBinding VerticalContentAlignment}"SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/></Border></ControlTemplate></Setter.Value></Setter></Style><!--行--><Style TargetType="DataGridRow"><Setter Property="Height" Value="35"/><Style.Triggers><Trigger Property="IsSelected" Value="True"><Setter Property="Background" Value="#C7CCD7"/></Trigger></Style.Triggers></Style><Style TargetType="DataGridCell"><Setter Property="Background" Value="Transparent"/><Style.Triggers><Trigger Property="IsSelected" Value="True"><Setter Property="Foreground" Value="Black"/><Setter Property="BorderBrush" Value="Transparent"/></Trigger></Style.Triggers></Style></ResourceDictionary>
  • 在Style新增字源詞典TextBox.xaml,設置表格內文本框的樣式,添加進App.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Style x:Key="DataGridTextBoxStyle" TargetType="TextBox"><Setter Property="HorizontalAlignment" Value="Center"/><Setter Property="VerticalAlignment" Value="Center"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="TextBox"><ScrollViewer x:Name="PART_ContentHost" Focusable="True" Margin="5"HorizontalScrollBarVisibility="Hidden"VerticalScrollBarVisibility="Visible"/></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary>

CustomerView.xaml界面代碼如下:

 <DataGrid ItemsSource="{Binding CustomerList}"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}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="姓名"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Name}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="電話"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Telephone}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="地址"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Address}" Style="{StaticResource DataGridTextBoxStyle}" HorizontalAlignment="Left"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid>
  • 實現效果如下:
    在這里插入圖片描述

3.3 顧客刪除

  • 在CustomView.xaml新增刪除當前顧客按鈕,CustomViewModel新增SelectedCustomer屬性作為刪除選中實體,DataGrid控件增加SelectedItem作為刪除選中的數據。
  • View文件夾新增Dialog.xaml作為刪除顧客時的提示框
<Window x:Class="超市管理系統.View.Dialog"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"WindowStyle="None"Background="#2F3640"AllowsTransparency="True"WindowStartupLocation="CenterScreen"Title="MessageDialog" Height="320" Width="540"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/><RowDefinition/><RowDefinition Height="auto"/><RowDefinition/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="&#xf00d;" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" Foreground="#2581FE" FontSize="30" FontFamily="/Fonts/#FontAwesome" MouseUp="TextBlock_MouseUp"/><TextBlock Grid.Row="1" Text="&#xf071;" HorizontalAlignment="center" VerticalAlignment="center" Margin="10" Foreground="#D25D56" FontSize="80" FontFamily="/Fonts/#FontAwesome"/><TextBlock Grid.Row="2" x:Name="textblock" Text="確定要刪除數據?刪除之后無法恢復!" HorizontalAlignment="center" VerticalAlignment="Center" Margin="10" FontFamily="/Fonts/#FontAwesome"/><Border Grid.Row="3" Background="#3E4450" Height="1"/><StackPanel Grid.Row="4" HorizontalAlignment="Right" VerticalAlignment="center" Orientation="Horizontal" Margin="10"><Button Content="確定" Style="{StaticResource ButtonDialogStyle}" Width="120" Height="40" Click="Button_ClickOK"/><Button Content="取消" Style="{StaticResource ButtonDialogStyle}" Width="120" Height="40" Click="Button_ClickCancel" Margin ="10 0 10 0"/></StackPanel></Grid>
</Window>

新增窗口內Button的樣式并添加進App.xaml中,代碼如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><!--提示對話框按鈕樣式--><Style x:Key="ButtonDialogStyle" TargetType="Button"><Setter Property="VerticalAlignment" Value="Center"/><Setter Property="FontSize" Value="18"/><Setter Property="Foreground" Value="#2383FC"/><Setter Property="Background" Value="Transparent"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Grid Background="{TemplateBinding Background}"><Border Background="Transparent" BorderBrush="#2383FC" BorderThickness="1"><TextBlock x:Name="textblock" Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}"FontSize="{TemplateBinding FontSize}"HorizontalAlignment="Center"VerticalAlignment="Center"Margin="5"/></Border></Grid><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" Value="#2383FC"/><Setter Property="Foreground" Value="White"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>
</ResourceDictionary>
  • Dialog.cs后臺代碼為
using System.Windows;
using System.Windows.Input;namespace 超市管理系統.View
{/// <summary>/// Dialog.xaml 的交互邏輯/// </summary>public partial class Dialog : Window{public static new bool Show(){Dialog dialog = new Dialog();var result = dialog.ShowDialog();return dialog.IsOK;}public bool IsOK = false;public Dialog(){InitializeComponent();}private void Button_ClickOK(object sender, RoutedEventArgs e){IsOK = true;Close();}private void Button_ClickCancel(object sender, RoutedEventArgs e){Close();}private void TextBlock_MouseUp(object sender, MouseButtonEventArgs e){Close();}}
}
  • 在CustomerViewModel新增刪除Command,最終實現效果如下
public RelayCommand<UserControl> DeleteCommand
{get{return new RelayCommand<UserControl>((view) =>{if(SelectedCustomer == null) {return; }if (Dialog.Show() == true){var count = customerProvider.Delete(SelectedCustomer);if (count > 0){MessageBox.Show("刪除成功");CustomerList = customerProvider.GetAll();}}});}
}

在這里插入圖片描述

3.4 顧客修改

  • 第一種修改方式
  • IProvider 增加接口int Save()
  • CustomerProvider.cs修正接口,新增函數如下:
        public int Save(){return db.SaveChanges();}
  • 在CustomView.xaml新增保存按鈕,將姓名、電話、地址四列改為TwoWay模式,UpdateSourceTrigger為PropertyChanged實現修改時發送通知,以姓名為例
<TextBox Text="{Binding Name,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/>
  • CustomViewModel新增SaveCommand命令實現保存功能
public RelayCommand<UserControl> SaveCommand
{get{return new RelayCommand<UserControl>((view) =>{var count = customerProvider.Save();if (count > 0){MessageBox.Show("保存成功");CustomerList = customerProvider.GetAll();}});}
}
  • 第二種修改方式
  • 選中要修改的對象,將對象值傳到另一個窗體中,實現原理根據新增顧客的模板修改,新增EditCustomerView.xaml
<Window x:Class="超市管理系統.View.EditCustomerView"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=EditCustomerViewModel}"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><StackPanel Grid.Row="1" Margin="10" HorizontalAlignment="Center" Width="500"><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="姓名:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Customer.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="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Customer.Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="地址:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Customer.Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" Height="30" VerticalAlignment="Center"/></StackPanel></StackPanel><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>
  • 新增EditCustomerViewModel.cs
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 超市管理系統.Entity;namespace 超市管理系統.ViewModel
{public class EditCustomerViewModel : ViewModelBase2{private CustomerProvider customerProvider = new CustomerProvider();private Customer customer;public Customer Customer{get { return customer; }set{customer = value;RaisePropertyChanged();}}public RelayCommand<Window> OKCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Customer.Name)){MessageBox.Show("姓名不能為空!");return;}if (string.IsNullOrEmpty(Customer.Telephone)){MessageBox.Show("地址不能為空!");return;}if (string.IsNullOrEmpty(Customer.Address)){MessageBox.Show("電話不能為空!");return;}//Customer.InsertDate = DateTime.Now;int count = customerProvider.Update(Customer);if (count > 0){MessageBox.Show("操作成功!");}view.DialogResult = true;view.Close();});}}public RelayCommand<Window> ExitCommand{get{return new RelayCommand<Window>((view) =>{Customer = new Customer();});}}}
}
  • 實現效果如下,彈窗內修改會實時顯示在主界面中
    在這里插入圖片描述

4. 供應商管理

  • 客戶管理與供應商管理的功能類似,且屬性都為Id、姓名、電話、地址。因此可把顧客管理的代碼拿來復用。此處可用到Visual Studio快捷操作,復制Supplier、supplier,在代碼選中Customer、customer,按住**shift + alt + ;**全部選中刪除,然后粘貼。

4.1 供應商管理主界面

  • SupplierView.xaml實現代碼如下:
<UserControl x:Class="超市管理系統.View.SupplierView"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=SupplierViewModel}"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 SupplierList}"SelectedItem="{Binding SelectedSupplier}"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}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="電話"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="地址"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Address, 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 SelectedSupplier.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>
  • SupplierViewModel.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 SupplierViewModel:ViewModelBase2{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 selectedSupplier;public Supplier SelectedSupplier{get { return selectedSupplier; }set{selectedSupplier = value;RaisePropertyChanged();}}#region commands/// <summary>/// 加載所有供應商/// </summary>public RelayCommand<UserControl> LoadedCommand{get{return new RelayCommand<UserControl>((view) =>{SupplierList = supplierProvider.GetAll();});}}public RelayCommand<UserControl> OpenAddViewCommand{get{return new RelayCommand<UserControl>((view) =>{AddSupplierView addSupplierView = new AddSupplierView();if (addSupplierView.ShowDialog().Value == true){SupplierList = supplierProvider.GetAll();}});}}public RelayCommand<UserControl> DeleteCommand{get{return new RelayCommand<UserControl>((view) =>{if (SelectedSupplier == null) { return; }if (Dialog.Show() == true){var count = supplierProvider.Delete(SelectedSupplier);if (count > 0){MessageBox.Show("刪除成功");SupplierList = supplierProvider.GetAll();}}});}}public RelayCommand<UserControl> SaveCommand{get{return new RelayCommand<UserControl>((view) =>{var count = supplierProvider.Save();if (count > 0){MessageBox.Show("保存成功");SupplierList = supplierProvider.GetAll();}});}}public RelayCommand<Window> EditCommand{get{return new RelayCommand<Window>((view) =>{if (SelectedSupplier == null) { return; }var vm = ServiceLocator.Current.GetInstance<EditSupplierViewModel>();vm.Supplier = SelectedSupplier;EditSupplierView editSupplierView = new EditSupplierView();if (editSupplierView.ShowDialog().Value == true){SupplierList = supplierProvider.GetAll();}});}}#endregion}
}

4.2 新增供應商

  • 增加供應商AddSupplierView.xaml代碼如下:
<Window x:Class="超市管理系統.View.AddSupplierView"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=AddSupplierViewModel}"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><StackPanel Grid.Row="1" Margin="10" HorizontalAlignment="Center" Width="500"><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="姓名:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Supplier.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="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Supplier.Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="地址:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Supplier.Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" Height="30" VerticalAlignment="Center"/></StackPanel></StackPanel><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>
  • 增加供應商AddSupplierViewModel.cs代碼如下:
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 超市管理系統.Entity;namespace 超市管理系統.ViewModel
{public class AddSupplierViewModel:ViewModelBase2{private SupplierProvider supplierProvider = new SupplierProvider();private Supplier supplier;public Supplier Supplier{get { return supplier; }set{supplier = value;RaisePropertyChanged();}}#region commandspublic RelayCommand<Window> LoadedCommand{get{return new RelayCommand<Window>((view) =>{Supplier = new Supplier();});}}public RelayCommand<Window> AddCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Supplier.Name)){MessageBox.Show("姓名不能為空!");return;}if (string.IsNullOrEmpty(Supplier.Telephone)){MessageBox.Show("地址不能為空!");return;}if (string.IsNullOrEmpty(Supplier.Address)){MessageBox.Show("電話不能為空!");return;}Supplier.InsertDate = DateTime.Now;int count = supplierProvider.Insert(Supplier);if (count > 0){MessageBox.Show("操作成功!");}view.DialogResult = true;view.Close();});}}public RelayCommand<Window> ExitCommand{get{return new RelayCommand<Window>((view) =>{Supplier = new Supplier();});}}#endregion}
}

4.3 修改供應商

  • 修改供應商EditSupplierView.xaml實現代碼如下:
<Window x:Class="超市管理系統.View.EditSupplierView"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=EditSupplierViewModel}"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><StackPanel Grid.Row="1" Margin="10" HorizontalAlignment="Center" Width="500"><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="姓名:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Supplier.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="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Supplier.Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="地址:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Supplier.Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="250" Height="30" VerticalAlignment="Center"/></StackPanel></StackPanel><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>

修改供應商EditSupplierViewModel.csl實現代碼如下:

using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 超市管理系統.Entity;namespace 超市管理系統.ViewModel
{public class EditSupplierViewModel:ViewModelBase2{private SupplierProvider supplierProvider = new SupplierProvider();private Supplier supplier;public Supplier Supplier{get { return supplier; }set{supplier = value;RaisePropertyChanged();}}#region commandspublic RelayCommand<Window> OKCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Supplier.Name)){MessageBox.Show("姓名不能為空!");return;}if (string.IsNullOrEmpty(Supplier.Telephone)){MessageBox.Show("地址不能為空!");return;}if (string.IsNullOrEmpty(Supplier.Address)){MessageBox.Show("電話不能為空!");return;}//Supplier.InsertDate = DateTime.Now;int count = supplierProvider.Update(Supplier);if (count > 0){MessageBox.Show("操作成功!");}view.DialogResult = true;view.Close();});}}public RelayCommand<Window> ExitCommand{get{return new RelayCommand<Window>((view) =>{Supplier = new Supplier();});}}#endregion}
}
  • 最后需要在ViewModelLocator.cs內新增ViewModel如下:
public ViewModelLocator()
{**SimpleIoc.Default.Register<AddSupplierViewModel>();SimpleIoc.Default.Register<EditSupplierViewModel>();**
}public AddSupplierViewModel AddSupplierViewModel => ServiceLocator.Current.GetInstance<AddSupplierViewModel>();public EditSupplierViewModel EditSupplierViewModel => ServiceLocator.Current.GetInstance<EditSupplierViewModel>(); 

在這里插入圖片描述

5. 用戶管理

  • 新建文件夾Enums,文件夾內新建LevelType存儲用戶Level
using System;namespace 超市管理系統.Enums
{public enum LevelType{游客 = 0,操作員 = 1,管理員 = 9}
}
  • 在Enums內新增Model文件夾,新增BaseMdel.cs基類添加LevelType。此處修改level為string類型,需在數據庫中將9改為管理員,并將Visual Studio中的Member表刪除并從模型中更新。
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using 超市管理系統.Enums;namespace 超市管理系統.Entity.Model
{/// <summary>/// 擴展實體基類/// </summary>public partial class BaseModel:ObservableObject{public List<string> Levels { get{List<string> levelTypes =  new List<string>();//levelTypes.Add("游客");//levelTypes.Add("操作員");//levelTypes.Add("管理員");var array = Enum.GetNames(typeof(LevelType));foreach (var type in array){levelTypes.Add(type.ToString());}return levelTypes;} }}
}
  • Model文件夾,新增Member.cs為Member的擴展類
using 超市管理系統.Entity.Model;namespace 超市管理系統.Entity
{public partial class Member:BaseModel{}
}

5.1 用戶管理主界面

  • MemberView.xaml內容復用CustomerView.xaml并將Customer修改為Member,將綁定的屬性改為Member屬性
<UserControl x:Class="超市管理系統.View.MemberView"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=MemberViewModel}"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 MemberList}"SelectedItem="{Binding SelectedMember}"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}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="密碼"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/></Grid></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn><DataGridTemplateColumn Width="auto" Header="等級"><DataGridTemplateColumn.CellTemplate><DataTemplate><Grid><TextBox Text="{Binding Level, 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 SelectedMember.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>
  • 修改MemberViewModel
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 MemberViewModel:ViewModelBase2{private MemberProvider memberProvider = new MemberProvider();private List<Member> memberList = new List<Member>();public List<Member> MemberList{get { return memberList; }set{memberList = value;RaisePropertyChanged();}}//當前選中的顧客實體private Member selectedMember;public Member SelectedMember{get { return selectedMember; }set{selectedMember = value;RaisePropertyChanged();}}public RelayCommand<UserControl> LoadedCommand{get{return new RelayCommand<UserControl>((view) =>{MemberList = memberProvider.GetAll();});}}public RelayCommand<UserControl> OpenAddViewCommand{get{return new RelayCommand<UserControl>((view) =>{AddMemberView addMemberView = new AddMemberView();if (addMemberView.ShowDialog().Value == true){MemberList = memberProvider.GetAll();}});}}public RelayCommand<UserControl> DeleteCommand{get{return new RelayCommand<UserControl>((view) =>{if (SelectedMember == null) { return; }if (Dialog.Show() == true){var count = memberProvider.Delete(SelectedMember);if (count > 0){MessageBox.Show("刪除成功");MemberList = memberProvider.GetAll();}}});}}public RelayCommand<UserControl> SaveCommand{get{return new RelayCommand<UserControl>((view) =>{var count = memberProvider.Save();if (count > 0){MessageBox.Show("保存成功");MemberList = memberProvider.GetAll();}});}}public RelayCommand<Window> EditCommand{get{return new RelayCommand<Window>((view) =>{if (SelectedMember == null) { return; }var vm = ServiceLocator.Current.GetInstance<EditMemberViewModel>();vm.Member = SelectedMember;EditMemberView editMemberView = new EditMemberView();if (editMemberView.ShowDialog().Value == true){MemberList = memberProvider.GetAll();}});}}}
}

5.2 新增用戶

  • 新增AddMemberView.xaml,復用AddCustomerView.xaml并修改。等級綁定Member.Levels
<Window x:Class="超市管理系統.View.AddMemberView"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=AddMemberViewModel}"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><StackPanel Grid.Row="1" Margin="10" HorizontalAlignment="Center" Width="500"><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="姓名:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Member.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="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Member.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="等級:" Width="100" FontSize="18" VerticalAlignment="Center"/><ComboBox ItemsSource="{Binding Member.Levels}" SelectedItem="{Binding Member.Level}"SelectedIndex="0" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel></StackPanel><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>
  • 新增AddMemberViewModel.xaml,復用AddCustomerViewModel.cs并修改。
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 超市管理系統.Entity;
using 超市管理系統.Enums;namespace 超市管理系統.ViewModel
{public class AddMemberViewModel:ViewModelBase2{private MemberProvider memberProvider = new MemberProvider();private Member member;public Member Member{get { return member; }set{member = value;RaisePropertyChanged();}}public RelayCommand<Window> LoadedCommand{get{return new RelayCommand<Window>((view) =>{Member = new Member();});}}public RelayCommand<Window> AddCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Member.Name)){MessageBox.Show("姓名不能為空!");return;}if (string.IsNullOrEmpty(Member.Password)){MessageBox.Show("密碼不能為空!");return;}if (string.IsNullOrEmpty(Member.Level)){MessageBox.Show("等級不能為空!");return;}Member.InsertDate = DateTime.Now;int count = memberProvider.Insert(Member);if (count > 0){MessageBox.Show("操作成功!");}view.DialogResult = true;view.Close();});}}public RelayCommand<Window> ExitCommand{get{return new RelayCommand<Window>((view) =>{Member = new Member();});}}}
}

5.3 修改用戶

  • 新增EditMemberView.xaml,復用EditCustomerView.xaml并修改。等級綁定Member.Levels
<Window x:Class="超市管理系統.View.EditMemberView"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=EditMemberViewModel}"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><StackPanel Grid.Row="1" Margin="10" HorizontalAlignment="Center" Width="500"><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="姓名:" Width="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Member.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="100" FontSize="18" VerticalAlignment="Center"/><TextBox Text="{Binding Member.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel><StackPanel Orientation="Horizontal" Height="30" Margin="0 5 0 10"><TextBlock Text="地址:" Width="100" FontSize="18" VerticalAlignment="Center"/><ComboBox ItemsSource="{Binding Member.Levels}" SelectedItem="{Binding Member.Level}"SelectedIndex="0" Width="200" Height="30" VerticalAlignment="Center"/></StackPanel></StackPanel><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>
  • 新增EditMemberViewModel.xaml,復用EditCustomerViewModel.xaml并修改。
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 超市管理系統.Entity;
using 超市管理系統.Enums;namespace 超市管理系統.ViewModel
{public class EditMemberViewModel:ViewModelBase2{private MemberProvider memberProvider = new MemberProvider();private Member member;public Member Member{get { return member; }set{member = value;RaisePropertyChanged();}}public RelayCommand<Window> OKCommand{get{return new RelayCommand<Window>((view) =>{if (string.IsNullOrEmpty(Member.Name)){MessageBox.Show("姓名不能為空!");return;}if (string.IsNullOrEmpty(Member.Password)){MessageBox.Show("密碼不能為空!");return;}if (string.IsNullOrEmpty(Member.Level)){MessageBox.Show("等級不能為空!");return;}//Member.InsertDate = DateTime.Now;int count = memberProvider.Update(Member);if (count > 0){MessageBox.Show("操作成功!");}view.DialogResult = true;view.Close();});}}public RelayCommand<Window> ExitCommand{get{return new RelayCommand<Window>((view) =>{Member = new Member();});}}}
}
  • 采用新增的用戶佟湘玉登錄,實現效果如下:
    在這里插入圖片描述

總結

  1. SQL Server往數據庫表里面插入數據時,提示:當 IDENTITY_INSERT 設置為 OFF 時,不能為表中的標識列插入顯式值。此錯誤為數據庫的主鍵為設置自增,當連續添加是主鍵為0無法插入。需去數據庫修改主鍵為自增,在Visual Studio中將該表刪除,然后從數據庫中更新表。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/90761.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/90761.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/90761.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Windows本地部署DeepSeek

1、Ollama1、下載Ollama安裝包https://ollama.com/download&#xff08;如果下載很慢 可以直接找我拿安裝包&#xff09;2、使用命令行安裝打開cmd 將下載的安裝包OllamaSetup.exe 放到想要安裝的目錄下。&#xff08;如果直接雙擊&#xff0c;會裝到C盤&#xff09;例如想裝到…

基于Python的新聞爬蟲:實時追蹤行業動態

引言 在信息時代&#xff0c;行業動態瞬息萬變。金融從業者需要實時了解政策變化&#xff0c;科技公司需要跟蹤技術趨勢&#xff0c;市場營銷人員需要掌握競品動向。傳統的人工信息收集方式效率低下&#xff0c;難以滿足實時性需求。Python爬蟲技術為解決這一問題提供了高效方…

阿里視頻直播解決方案VS(MediaMTX + WebRTC) 流媒體解決方案

背景&#xff1a; 公司采購了新的攝像頭&#xff0c;通過rtsp或者rtmp推流到云平臺&#xff0c;云平臺內部進行轉碼處理&#xff0c;客戶端使用HLS或HTTP-FLV播放&#xff0c;移動App可能使用HLS或私有SDK&#xff0c;超低延時則采用WebRTC。 技術選型&#xff1a; RTSP&…

day33:零基礎學嵌入式之網絡——TCP并發服務器

一、服務器1.服務器分類單循環服務器&#xff1a;只能處理一個客戶端任務的服務器并發服務器&#xff1a;可同時處理多個客戶端任務的服務器二、TCP并發服務器的構建1.如何構建&#xff1f;&#xff08;1&#xff09;多進程&#xff08;每一次創建都非常耗時耗空間&#xff0c;…

VR全景制作的流程?VR全景制作可以用在哪些領域?

VR全景制作的流程&#xff1f;VR全景制作可以用在哪些領域&#xff1f;VR全景制作&#xff1a;流程、應用與未來虛擬現實&#xff08;VR&#xff09;全景制作正迅速改變我們的感官體驗&#xff0c;使我們能夠身臨其境地探索虛擬世界&#xff0c;享受沉浸式的奇妙感受。那么&…

用LangChain重構客服系統:騰訊云向量數據庫+GPT-4o實戰

人們眼中的天才之所以卓越非凡&#xff0c;并非天資超人一等而是付出了持續不斷的努力。1萬小時的錘煉是任何人從平凡變成超凡的必要條件。———— 馬爾科姆格拉德威爾 目錄 一、傳統客服系統痛點與重構價值 1.1 傳統方案瓶頸分析 1.2 新方案技術突破點 二、系統架構設計&…

主要分布在腹側海馬體(vHPC)CA1區域(vCA1)的混合調諧細胞(mixed-tuning cells)對NLP中的深層語義分析的積極影響和啟示

腹側海馬體CA1區&#xff08;vCA1&#xff09;的混合調諧細胞&#xff08;mixed-tuning cells&#xff09;通過整合情感、社會關系、空間概念等多模態信息&#xff0c;形成動態的情景化語義表征&#xff0c;為自然語言處理&#xff08;NLP&#xff09;的深層語義分析提供了重要…

ESP32的ADF詳解:6. Audio Processing的API

一、Downmix 1. 核心功能 將基礎音頻流和新加入音頻流混合為單一輸出流&#xff0c;支持動態增益控制和狀態轉換。輸出聲道數與基礎音頻一致&#xff0c;新加入音頻自動轉換聲道匹配。2. 關鍵特性聲道處理 輸出聲道數 基礎音頻聲道數新加入音頻自動轉換聲道&#xff08;如立體…

Qt(基本組件和基本窗口類)

一、基本組件1. Designer設計師為什么要上來先將這個東西呢&#xff0c;這個是QT外置的設計界面的工具&#xff0c;沒啥用&#xff0c;所以了解一下。我們用的多的是QT內置的界面設計&#xff0c;只需要我們雙擊我們創建的項目的.ui文件就可以進入這個界面&#xff0c;你對界面…

docker與k8s的容器數據卷

Docker容器數據卷 特性 docker鏡像由多個只讀層疊加而成&#xff0c;啟動容器時&#xff0c;Docker會加載只讀鏡像層并在鏡像棧頂部添加一個讀寫層。如果運行中的容器修改了現有的一個已經存在的文件&#xff0c;那么該文件將會從讀寫層下面的只讀層復制到讀寫層&#xff0c;該…

自然語言處理技術應用領域深度解析:從理論到實踐的全面探索

1. 引言:自然語言處理的技術革命與應用前景 自然語言處理(Natural Language Processing,NLP)作為人工智能領域的核心分支,正在以前所未有的速度改變著我們的數字化生活。從最初的規則基礎系統到如今基于深度學習的大語言模型,NLP技術經歷了從理論探索到實際應用的深刻變…

OpenGLRender開發記錄(二): 陰影(shadowMap,PCF,PCSS)

目錄已實現功能陰影shadowMapPCFPCSS實現shadowMapPCFPCSS陰影GitHub主頁&#xff1a;https://github.com/sdpyy1 OpenGLRender:https://github.com/sdpyy1/CppLearn/tree/main/OpenGL 已實現功能 除了上次實現IBL之外&#xff0c;項目目前新增了imGUI的渲染&#xff0c;更方便…

Linux:日志亂碼

1、Linux日志亂碼可能是XShell客戶端編碼沒設置為UTF-8引起的&#xff0c;按照以下步驟&#xff0c;設置終端格式&#xff1a;中文版&#xff1a;打開Xshell會話屬性&#xff08;文件→屬性→終端→編碼&#xff09;&#xff0c;選擇與服務器一致的編碼格式&#xff08;如UTF-8…

Rouge:面向摘要自動評估的召回導向型指標——原理、演進與應用全景

“以n-gram重疊量化文本生成質量&#xff0c;為摘要評估提供可計算標尺” Rouge&#xff08;Recall-Oriented Understudy for Gisting Evaluation&#xff09; 是由 南加州大學信息科學研究所&#xff08;ISI&#xff09;的Chin-Yew Lin 于2004年提出的自動文本摘要評估指標&am…

[STM32][HAL]stm32wbxx 超聲波測距模塊實現(HY-SRF05)

前言 在電子技術應用中,距離測量是一個常見且重要的需求。超聲波模塊因其測量精度較高、成本較低、易于使用等優點,被廣泛應用于機器人避障、液位檢測、智能停車系統等領域。該文主要講解以stm32wb芯片為主控,用HAL庫來對HY-SRF05超聲波模塊進行代碼編寫,實現基本的驅動和測…

MySQL 性能調優實戰指南:從診斷到優化全解析

引言在日常的數據庫運維工作中&#xff0c;我們經常需要對 MySQL 數據庫進行診斷和性能分析。本文將介紹一套全面的 MySQL 診斷腳本&#xff0c;適用于 MySQL 8.0&#xff08;兼容 8.0.15 及以上版本&#xff09;&#xff0c;涵蓋事務鎖分析、性能瓶頸定位、配置檢查、連接狀態…

8. 狀態模式

目錄一、應用背景二、狀態模式2.1 解決的問題2.2 角色2.3 實現步驟三、通用設計類圖四、實現4.1 設計類圖4.2 狀態轉換圖4.3 代碼實現一、應用背景 某對象發生變化時&#xff0c;其所能做的操作也隨之變化。應用程序的可維護性和重用性差代碼的邏輯較復雜 二、狀態模式 2.1 …

php語法--foreach和in_array的使用

文章目錄foreach基礎語法&#xff1a;案例1&#xff1a;引用傳遞模式&#xff1a;嵌套數組處理&#xff1a;避免在循環中計算數組長度&#xff1a;使用引用減少內存拷貝&#xff1a;打印數組in_array基礎使用嚴格使用foreach 基礎語法&#xff1a; foreach ($iterable as $va…

ES6模塊詳解:核心語法與最佳實踐

以下是 EMAScript 6&#xff08;ES6&#xff09;模塊規范的核心要點及細節解析&#xff1a; &#x1f4e6; 一、核心語法導出&#xff08;export&#xff09; 命名導出&#xff1a;支持導出多個具名成員。export const a 1; export function b() { /* ... */ } // 或集中導出 …

Python day25

浙大疏錦行 Python day25. 內容&#xff1a; 異常處理&#xff0c;在日常的編碼工作過程中&#xff0c;為了避免由于各種bug導致的異常情況&#xff0c;我們需要引入異常處理機制&#xff0c;它的工作場景是當程序運行出現意外時&#xff0c;可以根據編碼規則處理響應的錯誤。…