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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈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;可以根据编码规则处理响应的错误。…