原创不易,打字不易,截图不易,多多点赞,送人玫瑰,留有余香,财务自由明日实现。
1、创建物资入库用户控件
<UserControl x:Class="West.StoreMgr.View.InStoreView"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:West.StoreMgr.View"mc:Ignorable="d" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"DataContext="{Binding Source={StaticResource Locator},Path=InStore}" ><i:Interaction.Triggers><i:EventTrigger EventName="Loaded"><i:InvokeCommandAction Command="{Binding LoadCommand}"/></i:EventTrigger></i:Interaction.Triggers><Grid><Grid.RowDefinitions><RowDefinition Height="50"/><RowDefinition/><RowDefinition/></Grid.RowDefinitions><!--标题--><StackPanel Background="#EDF0F6" Orientation="Horizontal"><TextBlock Margin="10 0 0 0" Text="" FontSize="20" FontFamily="/Fonts/#FontAwesome" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#797672"/><TextBlock Margin="10 0 0 0" Text="首页 > 物资入库" FontSize="20" FontFamily="/Fonts/#FontAwesome" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="#797672"/></StackPanel><!--增加--><Grid Grid.Row="1" Margin="20"><Grid.RowDefinitions><RowDefinition Height="30"/><RowDefinition/></Grid.RowDefinitions><Border Background="#72BBE5"><TextBlock Text="添加数据" FontSize="18" VerticalAlignment="Center" Foreground="#1F3C4C" Margin="0 0 10 0"/></Border><StackPanel Grid.Row="1" Orientation="Vertical" VerticalAlignment="Center" Margin="0 10 0 10"><StackPanel Orientation="Horizontal" VerticalAlignment="Center"><TextBlock Margin="0 0 10 0" Text="选择仓库:" VerticalAlignment="Center" Width="80"/><ComboBox x:Name="comboboxGoodsType" Margin="0 0 10 0" ItemsSource="{Binding StoreList}" SelectedItem="{Binding Store}" DisplayMemberPath="Name" SelectedValuePath="Id" Width="150" Height="30" /><TextBlock Margin="0 0 10 0" Text="选择供应商:" VerticalAlignment="Center" Width="80"/><ComboBox x:Name="comboboxSpec" Margin="0 0 10 0" ItemsSource="{Binding SupplierList}" SelectedItem="{Binding Supplier}" DisplayMemberPath="Name" SelectedValuePath="Id" Width="150" Height="30" /></StackPanel><StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0 10 0 10"><Button Content="选择入库物资..." Command="{Binding OpenSelectGoodsWindow}"/><TextBlock Margin="0 0 10 0" Text=" 序号:" VerticalAlignment="Center" Width="80"/><TextBlock Margin="0 0 10 0" Text="{Binding InStore.GoodsSerial,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" /><TextBlock Margin="0 0 10 0" Text="物资名称:" VerticalAlignment="Center" Width="80"/><TextBlock Margin="0 0 10 0" Text="{Binding InStore.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" /><TextBlock Margin="30 0 10 0" Text="单位:" VerticalAlignment="Center" Width="auto"/><TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding InStore.Unit,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" /></StackPanel><StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0 10 0 10"><TextBlock Margin="0 0 10 0" Text="入库数量:" VerticalAlignment="Center" Width="80"/><TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding InStore.Number,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" /><TextBlock Margin="0 0 10 0" Text="入库价格:" VerticalAlignment="Center" Width="80"/><TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding InStore.Price,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" /><TextBlock Margin="0 0 10 0" Text="备注:" VerticalAlignment="Center" Width="80"/><TextBox HorizontalAlignment="Left" VerticalAlignment="Center" TextAlignment="Left" VerticalContentAlignment="Center" Margin="0 0 10 0" Text="{Binding InStore.Tag,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="150" Height="30" /></StackPanel><StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="0 10 0 10"> <!--button--><Button Margin="542 0 0 0" Height="36" Width="199" Grid.Row="3" FontSize="20"Content="增 加" Style="{StaticResource ButtonStyle}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:InStoreView}}"Command="{Binding AddCommand}"/></StackPanel></StackPanel> </Grid><!--浏览--><Grid Grid.Row="2" Margin="10 0 10 10"><DataGrid ItemsSource="{Binding InStoreList}" CanUserDeleteRows="False" CanUserAddRows="False" AutoGenerateColumns="False"><DataGrid.Columns><DataGridTextColumn Header="序号" Binding="{Binding GoodsSerial}"/><DataGridTextColumn Header="名称" Binding="{Binding Name}"/><DataGridTextColumn Header="仓库" Binding="{Binding StoreName}"/><DataGridTextColumn Header="供应商" Binding="{Binding SupplierName}"/><DataGridTextColumn Header="入库数量" Binding="{Binding Number}"/><DataGridTextColumn Header="入库价格" Binding="{Binding Price}" IsReadOnly="True"/><DataGridTextColumn Header="备注" Binding="{Binding Tag}"/><DataGridTextColumn Header="日期" Binding="{Binding InsertDate}"/><DataGridTemplateColumn Header="操作"><DataGridTemplateColumn.CellTemplate><DataTemplate><StackPanel Orientation="Horizontal"><Button Content="编辑" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:GoodsView},Path=DataContext.EditCommand}"CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}" Tag="{Binding}" Style="{StaticResource DataGridButtonStyle}" /><Button Content="删除" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=local:GoodsView},Path=DataContext.DeleteCommand}"CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}}"Tag="{Binding}" Style="{StaticResource DataGridButtonStyle}" /></StackPanel></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn></DataGrid.Columns></DataGrid></Grid></Grid>
</UserControl>
2、创建视图模型InStoreViewModel
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using West.StoreMgr.Helper;
using West.StoreMgr.View;
using West.StoreMgr.Service;
using West.StoreMgr.Model;
using West.StoreMgr.Windows;
using West.StoreMgr.Helper.AutoMapper;namespace West.StoreMgr.ViewModel
{/// <summary>/// 物资入库viewmodel/// </summary>public class InStoreViewModel : ViewModelBase{//添加public RelayCommand<UserControl> AddCommand{get{var command = new RelayCommand<UserControl>((obj) =>{if (!(obj is InStoreView view)) return;if (string.IsNullOrEmpty(InStore.Name) == true|| string.IsNullOrEmpty(InStore.GoodsSerial) == true){MsgWinHelper.ShowError("序号和名字不能为空");return;}InStore.InsertDate = DateTime.Now;InStore.UserInfoId = AppData.Instance.User.Id;if (Store.Id == 0){MsgWinHelper.ShowError("仓库不能为空");return;}if (Supplier.Id == 0){MsgWinHelper.ShowError("供应商不能为空");return;}InStore.StoreId = Store.Id;InStore.StoreName = Store.Name;InStore.SupplierId = Supplier.Id;InStore.SupplierName = Supplier.Name;//实体映射 var newInstore = Configration.Mapper.Map<InStore>(InStore);var service = new InStoreService();int count = service.Insert(newInstore);if (count > 0){//及时更新当前物资的存量if (Goods != null){Goods.Quant += InStore.Number;GoodsService goodsService = new GoodsService();count = goodsService.Update(this.Goods);}InStoreList = new InStoreService().Select();InStore = new InStoreEx();MsgWinHelper.ShowMessage("操作成功"); }else{MsgWinHelper.ShowError("操作失败");}});return command;}}//加载数据public RelayCommand LoadCommand{get{return new RelayCommand(() =>{StoreList = new StoreService().Select();SupplierList = new SupplierService().Select();InStoreList = new InStoreService().Select();});}}private Store store = new Store();/// <summary>/// 当前仓库/// </summary>public Store Store{get { return store; }set { store = value; RaisePropertyChanged(); }}private List<Store> storeList = new List<Store>();/// <summary>/// 仓库集合/// </summary>public List<Store> StoreList{get { return storeList; }set { storeList = value; RaisePropertyChanged(); }} private Supplier supplier = new Supplier();/// <summary>/// 当前供应商/// </summary>public Supplier Supplier{get { return supplier; }set { supplier = value; RaisePropertyChanged(); }} private List<Supplier> supplierList = new List<Supplier>();/// <summary>/// 供应商集合/// </summary>public List<Supplier> SupplierList{get { return supplierList; }set { supplierList = value; RaisePropertyChanged(); }}private InStoreEx instore = new InStoreEx();public InStoreEx InStore{get { return instore; }set { instore = value; RaisePropertyChanged(); }}private List<InStore> inStoreList = new List<InStore>();/// <summary>/// 入库历史记录/// </summary>public List<InStore> InStoreList{get { return inStoreList; }set { inStoreList = value; RaisePropertyChanged(); }}private Goods goods = new Goods();/// <summary>/// 当前选择的资物对象/// </summary>public Goods Goods{get { return goods; }set { goods = value; RaisePropertyChanged(); }}public RelayCommand OpenSelectGoodsWindow{get{return new RelayCommand(() =>{var window = new SelectGoodsWindow();var result = window.ShowDialog();if (result.HasValue && result.Value == true){var vm = window.DataContext as SelectGoodsViewModel;InStore.GoodsSerial = vm.Goods.Serial;InStore.Name = vm.Goods.Name;this.Goods = vm.Goods;}});}}}
}
3、选择物资窗体
4、选择物资viewmodel
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using West.StoreMgr.Service;namespace West.StoreMgr.ViewModel
{/// <summary>/// 选择物资viewmodel/// </summary>public class SelectGoodsViewModel : ViewModelBase{/// <summary>///窗体加载命令/// </summary>public RelayCommand LoadCommand{get{return new RelayCommand(() =>{GoodsList = new GoodsService().Select();});}}private Goods goods = new Goods();/// <summary>/// 物资/// </summary>public Goods Goods{get { return goods; }set { goods = value; RaisePropertyChanged(); }}private List<Goods> goodsList = new List<Goods>();/// <summary>/// 物资列表/// </summary>public List<Goods> GoodsList{get { return goodsList; }set { goodsList = value; RaisePropertyChanged(); }}}
}
5、运行效果
原创不易,打字不易,截图不易,多多点赞,送人玫瑰,留有余香,财务自由明日实现。