原創不易,打字不易,截圖不易,多多點贊,送人玫瑰,留有余香,財務自由明日實現。??
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、運行效果
?
?
?
原創不易,打字不易,截圖不易,多多點贊,送人玫瑰,留有余香,財務自由明日實現。??