一、 项目创建
1. 数据库设置
建立ShoppingDB数据库,采用默认设置,建立Member表,字段为Id(主键int)、Name(nvarchar50)、Password(nvarchar50)、Level(nvarchar50)、InsertDate(datetime) Id字段设置为是标识 ,标识增量为1 数据库建立其他表与表结构如下
2. 数据库连接
在VS界面建立Entity(截图为DAL)文件夹,并添加ADO.NET实体,命名为ShoppingModel 选择来自数据库的EF设计器 新建连接,MicroSoft SQL Server。有可能提示缺少文件,点击下一步确定安装。 服务器名称检索不到的话可以直接输入. ,代表本地服务器。数据库名称可检索到刚建立的ShoppingDB 勾选是 勾选表,点击完成
3. 构造供货商Provider
c#创建接口IProvider 设置接口IProvider为泛型,限制泛型为class 建立数据库的增删改查函数 Entity增加ProvideBase类,设置为abstract类 Entity增加MemberProvide类,继承自ProviderBase与IProvider,IProvider泛型为Member,实现接口
在ProviderBase类中定义字段 public ShoppingDBEntities db = new ShoppingDBEntities(); 修改MemberProvider类,建立增删改查代码如下
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
namespace 超市管理系统. Entity
{
public class MemberProvider : ProviderBase , Iprovider< Member>
{
public int Delete ( Member entity)
{
db. Entry ( entity) . State = System. Data. Entity. EntityState. Deleted;
return db. SaveChanges ( ) ;
}
public List< Member> GetAll ( )
{
return db. Member. ToList ( ) ;
}
public int Insert ( Member entity)
{
db. Entry ( entity) . State = System. Data. Entity. EntityState. Added;
return db. SaveChanges ( ) ;
}
public int Update ( Member entity)
{
db. Entry ( entity) . State = System. Data. Entity. EntityState. Modified;
return db. SaveChanges ( ) ;
}
}
}
Entity增加ProductProvider类,继承自ProviderBase与IProvider,IProvider泛型为Product 修改MemberProvider类,建立增删改查代码如下
using System ;
using System. Collections. Generic ;
using System. Linq ;
using System. Text ;
using System. Threading. Tasks ;
namespace 超市管理系统. Entity
{
public class ProductProvider : ProviderBase , Iprovider< Product>
{
ProductProviderpublic int Delete ( Product entity)
{
db. Entry ( entity) . State = System. Data. Entity. EntityState. Deleted;
return db. SaveChanges ( ) ;
}
public List< Product > GetAll ( )
{
return db. Product. ToList ( ) ;
}
public int Insert ( Product entity)
{
db. Entry ( entity) . State = System. Data. Entity. EntityState. Added;
return db. SaveChanges ( ) ;
}
public int Update ( Product entity)
{
db. Entry ( entity) . State = System. Data. Entity. EntityState. Modified;
return db. SaveChanges ( ) ;
}
}
}
数据库其他表继承自ProviderBase与IProvider建立*****Provider对应的类,并增加增删改查代码。
二、 登录窗口
1. 窗口设置
数据库内Member表,点击编辑前200行 可在界面增加内容,此处增加1,admin,123,9,null VS界面创建View文件夹,创建LoginView窗体,在ViewModel文件夹内创建LoginViewModel类,并继承ViewModelBase,ViewModelBase已实现消息通知 改造 ViewModelLocator类,在原内容基础上仿照MainViewModel的写法增加以下
public class ViewModelLocator
{
public ViewModelLocator ( )
{
SimpleIoc. Default. Register < LoginViewModel> ( ) ;
}
public LoginViewModel LoginViewModel
{
get
{
return ServiceLocator. Current. GetInstance < LoginViewModel> ( ) ;
}
}
}
LoginView.xaml内增加数据上下文来源 DataContext=“{Binding Source={StaticResource Locator}, Path=LoginViewModel}” 在app.xaml中修改启动窗口为登录窗口 StartupUri=“View/LoginView.xaml” 在根目录下创建类AppData.cs,并继承ObservableObject ,使用懒汉Lazy模式,创造唯一实例 AppData.cs创建属性 public Member CurrentUser { get; set; } = new Member();
public class AppData : ObservableObject
{
public static AppData Instance{ get ; set ; } = new Lazy< AppData> ( ( ) => new AppData ( ) ) . Value;
public Member CurrentUser { get ; set ; } = new Member ( ) ;
}
LoginViewModel.cs内创建的当前用户实体为只读属性 public AppData AppData => AppData.Instance;" 创建Member类的用户实体字段和属性
private Member member = new Member ( ) { Name = "admin" , Password= "123" } ;
public Member Member
{
get { return member; }
set
{
member = value ;
RaisePropertyChanged ( ) ;
}
}
创建登录按钮和退出按钮的命令 LoginView.xaml界面的window命名loginView,按钮绑定命令使ElementName = loginView
#region commands
public RelayCommand< LoginView> LoginCommand
{
get
{
return new RelayCommand< LoginView> ( ( view) =>
{
if ( string . IsNullOrEmpty ( member. Name) && string . IsNullOrEmpty ( member. Password) )
{
return ;
}
var list = memberProvider. GetAll ( ) ;
var model = list. FirstOrDefault ( t => t. Name == member. Name && t. Password == member. Password) ;
if ( model != null )
{
AppData. CurrentUser = model;
}
else
{
MessageBox. Show ( "用户名或密码错误!" ) ;
new MainWindow ( ) . Show ( ) ;
}
} ) ;
}
}
public RelayCommand< LoginView> ExitCommand
{
get
{
return new RelayCommand< LoginView> ( ( view) =>
{
view. Close ( ) ;
} ) ;
}
set ;
}
#endregion
< Window x: Class= " 超市管理系统.View.LoginView"
x: Name= " loginView"
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"
WindowStartupLocation = " CenterScreen"
DataContext = " {Binding Source={StaticResource Locator}, Path=LoginViewModel}"
Title = " 系统登录" Height = " 250" Width = " 450" >
< StackPanel VerticalAlignment = " Center" HorizontalAlignment = " Center" >
< DockPanel>
< TextBlock Text = " 用户名:" Width = " 50" VerticalAlignment = " Center" Height = " 25" />
< TextBox x: Name= " userNameTextBox" Text = " {Binding Member.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 100" />
</ DockPanel>
< DockPanel Margin = " 0 15 0 0" >
< TextBlock Text = " 密 码:" Width = " 50" VerticalAlignment = " Center" Height = " 25" />
< TextBox x: Name= " passwordTextBox" Text = " {Binding Member.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 100" />
</ DockPanel>
< StackPanel Orientation = " Horizontal" HorizontalAlignment = " Right" Margin = " 0 15 0 0" >
< Button x: Name= " dengLu" Command = " {Binding LoginCommand}"
CommandParameter = " {Binding ElementName=loginView}"
Content = " 登录" Width = " 60" Height = " 25" />
< Button x: Name= " tuiChu" Command = " {Binding ExitCommand}"
CommandParameter = " {Binding ElementName=loginView}"
Content = " 退出" Width = " 60" Height = " 25" Margin = " 30 0 0 0" />
</ StackPanel>
</ StackPanel>
</ Window>
由于在MvvmLight中已存在ViewModelBase,在ViewModel文件夹创建ViewModelBase2.cs ,继承ViewModelBase 因MainView需显示当前登录用户,需重新在MainViewModel内同样写 public AppData AppData => AppData.Instance;" ,因此作出修改LoginViewModel和MainViewModel重新继承自ViewModelBase2 ,将 public AppData AppData => AppData.Instance;" 放在ViewModelBase2内,LoginViewModel删去该行代码,MainViewModel也不需要再写该行代码。
2. 优化登录页面布局
使用Colors Lite 抓取素材颜色数值,作为背景色。 在AppData类内添加全局变量内容:系统名称、背景色、前景色。
public Member CurrentUser { get ; set ; } = new Member ( ) ;
public string Title => "馒头超市管理系统" ;
public SolidColorBrush Background => new SolidColorBrush ( Color. FromRgb ( 40 , 53 , 81 ) ) ;
public SolidColorBrush Forgeround => new SolidColorBrush ( Colors. White) ;
LoginView.xaml界面的window.title、TextBlock的背景色并使用Effect、前景色都改为Bingding
< Window x: Class= " 超市管理系统.View.LoginView"
x: Name= " loginView"
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"
Background = " {Binding AppData.Background}"
WindowStartupLocation = " CenterScreen"
ResizeMode = " NoResize"
DataContext = " {Binding Source={StaticResource Locator}, Path=LoginViewModel}"
Title = " 系统登录" Height = " 400" Width = " 800" >
< Grid>
< Grid.RowDefinitions >
< RowDefinition Height = " 150" />
< RowDefinition/>
</ Grid.RowDefinitions>
< TextBlock Grid.Row = " 0" Text = " {Binding AppData.Title}" VerticalAlignment = " Center" HorizontalAlignment = " Center" Foreground = " White" FontSize = " 30" >
< TextBlock.Effect>
< DropShadowEffect/>
</ TextBlock.Effect>
</ TextBlock>
< StackPanel Grid.Row = " 1" HorizontalAlignment = " Center" VerticalAlignment = " Top" >
< DockPanel>
< TextBlock Text = " 用户名:" Width = " 50" VerticalAlignment = " Center" Foreground = " {Binding AppData.Forgeround}" Height = " 25" />
< TextBox x: Name= " userNameTextBox" Text = " {Binding Member.Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 100" />
</ DockPanel>
< DockPanel Margin = " 0 15 0 0" >
< TextBlock Text = " 密 码:" Width = " 50" VerticalAlignment = " Center" Foreground = " White" Height = " 25" />
< TextBox x: Name= " passwordTextBox" Text = " {Binding Member.Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width = " 100" />
</ DockPanel>
< StackPanel Orientation = " Horizontal" HorizontalAlignment = " Right" Margin = " 0 15 0 0" >
< Button x: Name= " dengLu" Command = " {Binding LoginCommand}"
CommandParameter = " {Binding ElementName=loginView}"
Content = " 登录" Width = " 60" Height = " 25" />
< Button x: Name= " tuiChu" Command = " {Binding ExitCommand}"
CommandParameter = " {Binding ElementName=loginView}"
Content = " 退出" Width = " 60" Height = " 25" Margin = " 30 0 0 0" />
</ StackPanel>
</ StackPanel>
</ Grid>
</ Window>
三、 系统开发
1.菜单设计
< ResourceDictionary xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns: x= " http://schemas.microsoft.com/winfx/2006/xaml" >
< Style x: Key= " MenuRadioButtonStyle" TargetType = " RadioButton" >
<Setter Property="Foreground" Value="#eeefff" />
<Setter Property="FontSize" Value="16" />
<Setter Property="Template" >
<Setter.Value>
<ControlTemplate TargetType="RadioButton" >
<!--此时已将radio的模板形状设置为了border,将border的背景色绑定radiobutton背景色,触发器才会生效-->
<Border Background=" { TemplateBinding Background} ">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text=" { TemplateBinding Tag} " FontFamily=" /Fonts/#FontAwesome" Margin=" 5"/>
<TextBlock Grid.Column="1" Text=" { TemplateBinding Content} " Margin=" 5"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<!--触发器的目标是radiobutton的背景色-->
<Trigger Property="IsMouseOver" Value="True" >
<Setter Property="Background" Value="red" />
</Trigger>
<Trigger Property="IsMouseOver" Value="False" >
<!--透明色-->
<Setter Property="Background" Value="Transparent" />
</Trigger>
<Trigger Property="IsChecked" Value="True" >
<Setter Property="Background" Value="Goldenrod" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</ Style>
</ ResourceDictionary>
< Window x: Class= " 超市管理系统.MainWindow"
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:超市管理系统" xmlns: view= " clr-namespace:超市管理系统.View"
mc: Ignorable= " d"
Title = " 馒头超市管理系统" Height = " 800" Width = " 1200"
Background = " {Binding AppData.Background}"
DataContext = " {Binding Source={StaticResource Locator}, Path=Main}" >
< Grid>
< Grid.RowDefinitions>
< RowDefinition/>
< RowDefinition Height = " auto" />
</ Grid.RowDefinitions>
< Grid Grid.Row = " 0" >
< Grid.ColumnDefinitions>
< ColumnDefinition Width = " 150" />
< ColumnDefinition Width = " 1" />
< ColumnDefinition Width = " *" />
</ Grid.ColumnDefinitions>
< Grid Grid.Column = " 0" >
< Grid.RowDefinitions>
< RowDefinition Height = " auto" />
< RowDefinition/>
</ Grid.RowDefinitions>
< StackPanel Orientation = " Horizontal" Grid.Row = " 0" >
< TextBlock Text = "  " FontFamily = " /Fonts/#FontAwesome" Margin = " 10" FontSize = " 30" >
< TextBlock.Foreground>
< LinearGradientBrush>
< GradientStop Offset = " 0" Color = " #C2F486" />
< GradientStop Offset = " 1" Color = " Red" />
</ LinearGradientBrush>
</ TextBlock.Foreground>
</ TextBlock>
< TextBlock Text = " {Binding AppData.Title}" Margin = " 0" Foreground = " White" FontSize = " 20" VerticalAlignment = " Center" />
</ StackPanel>
< StackPanel Grid.Row = " 1" >
< RadioButton x: Name= " IndexView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 首页" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " OrderView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 商品下单" Tag = "  " Checked = " View_Checked" />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 商品管理" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 顾客管理" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 供应商管理" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 出库管理" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 入库管理" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 订单详情" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 用户管理" Tag = "  " />
< RadioButton Style = " { StaticResource MenuRadioButtonStyle} " Content = " 系统设置" Tag = "  " />
</ StackPanel>
</ Grid>
< Border Grid.Column = " 1" Background = " #22304B" />
< ContentControl Grid.Column = " 2" x: Name= " container" >
< view: IndexView/>
</ ContentControl>
</ Grid>
< StatusBar Grid.Row = " 1" >
< StatusBarItem>
< TextBlock Text = " 当前用户:" />
</ StatusBarItem>
< StatusBarItem>
< TextBlock Text = " {Binding AppData.CurrentUser.Name}" />
</ StatusBarItem>
</ StatusBar>
</ Grid>
</ Window>
2. 界面切换
在View文件夹创建UserControl用户控件IndexView.xaml代表首页界面,ViewModel文件夹创建IndexViewModel.cs,在ViewModelLocator中按格式添加IndexViewModel 采用相同的方法依次为每个菜单按钮完成上述操作
using CommonServiceLocator ;
using GalaSoft. MvvmLight ;
using GalaSoft. MvvmLight. Ioc ;
using 超市管理系统. ViewModel;
namespace 超市管理系统. ViewModel
{
public class ViewModelLocator
{
public ViewModelLocator ( )
{
ServiceLocator. SetLocatorProvider ( ( ) => SimpleIoc. Default) ;
SimpleIoc. Default. Register < MainViewModel> ( ) ;
SimpleIoc. Default. Register < LoginViewModel> ( ) ;
SimpleIoc. Default. Register < IndexViewModel> ( ) ;
SimpleIoc. Default. Register < OrderViewModel> ( ) ;
SimpleIoc. Default. Register < CustomerViewModel> ( ) ;
SimpleIoc. Default. Register < GoodsViewModel> ( ) ;
SimpleIoc. Default. Register < InstorageViewModel> ( ) ;
SimpleIoc. Default. Register < OutstorageViewModel> ( ) ;
SimpleIoc. Default. Register < OrderDetailViewModel> ( ) ;
SimpleIoc. Default. Register < SettingViewModel> ( ) ;
SimpleIoc. Default. Register < MemberViewModel> ( ) ;
SimpleIoc. Default. Register < SupplierViewModel> ( ) ;
}
public MainViewModel Main
{
get
{
return ServiceLocator. Current. GetInstance < MainViewModel> ( ) ;
}
}
public LoginViewModel LoginViewModel
{
get
{
return ServiceLocator. Current. GetInstance < LoginViewModel> ( ) ;
}
}
public IndexViewModel IndexViewModel => ServiceLocator. Current. GetInstance < IndexViewModel> ( ) ;
public OrderViewModel OrderViewModel => ServiceLocator. Current. GetInstance < OrderViewModel> ( ) ;
public CustomerViewModel CustomerViewModel => ServiceLocator. Current. GetInstance < CustomerViewModel> ( ) ;
public GoodsViewModel GoodsViewModel => ServiceLocator. Current. GetInstance < GoodsViewModel> ( ) ;
public InstorageViewModel InstorageViewModel => ServiceLocator. Current. GetInstance < InstorageViewModel> ( ) ;
public OutstorageViewModel OutstorageViewModel => ServiceLocator. Current. GetInstance < OutstorageViewModel> ( ) ;
public OrderDetailViewModel OrderDetailViewModel => ServiceLocator. Current. GetInstance < OrderDetailViewModel> ( ) ;
public SettingViewModel SettingViewModel => ServiceLocator. Current. GetInstance < SettingViewModel> ( ) ;
public MemberViewModel MemberViewModel => ServiceLocator. Current. GetInstance < MemberViewModel> ( ) ;
public SupplierViewModel SupplierViewModel => ServiceLocator. Current. GetInstance < SupplierViewModel> ( ) ;
public static void Cleanup ( )
{
}
}
}
更新MemberView.xaml文件中的菜单Style、Name和Checked。
< StackPanel Grid.Row = " 1" >
< RadioButton x: Name= " IndexView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 首页" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " OrderView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 商品下单" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " GoodsView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 商品管理" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " CustomerView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 顾客管理" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " SupplierView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 供应商管理" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " InstorageView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 入库管理" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " OutstorageView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 出库管理" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " OrderDetailView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 订单详情" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " MemberView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 用户管理" Tag = "  " Checked = " View_Checked" />
< RadioButton x: Name= " SettingView" Style = " { StaticResource MenuRadioButtonStyle} " Content = " 系统设置" Tag = "  " Checked = " View_Checked" />
</ StackPanel>
通过Switch和反射的方法都可以实现点击进行切换
using System. Windows ;
using System. Windows. Controls ;
using 超市管理系统. View;
namespace 超市管理系统
{
public partial class MainWindow : Window
{
public MainWindow ( )
{
InitializeComponent ( ) ;
}
private void View_Checked ( object sender, RoutedEventArgs e)
{
if ( sender is RadioButton radioButton)
{
if ( string . IsNullOrEmpty ( radioButton. Name) ) return ;
Type type = Type. GetType ( "超市管理系统.View." + radioButton. Name) ;
container. Content = Activator. CreateInstance ( type) ;
}
}
}
}
}
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 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 顾客新增