超市管理系統
- 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="" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="10" Foreground="#2581FE" FontSize="30" FontFamily="/Fonts/#FontAwesome" MouseUp="TextBlock_MouseUp"/><TextBlock Grid.Row="1" Text="" 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();});}}}
}
- 采用新增的用戶佟湘玉登錄,實現效果如下:
總結
- SQL Server往數據庫表里面插入數據時,提示:當 IDENTITY_INSERT 設置為 OFF 時,不能為表中的標識列插入顯式值。此錯誤為數據庫的主鍵為設置自增,當連續添加是主鍵為0無法插入。需去數據庫修改主鍵為自增,在Visual Studio中將該表刪除,然后從數據庫中更新表。