private void Create() { Battery editItem = new Battery(); //实例化一个新的model BatteryEditViewModel bevm = new BatteryEditViewModel(editItem, _batteryTypeServie.Items); //实例化一个新的view model bevm.DisplayName = "Battery-Create"; bevm.commandType = CommandType.Create; var BatteryViewInstance = new BatteryView(); //实例化一个新的view BatteryViewInstance.DataContext = bevm; BatteryViewInstance.ShowDialog(); //设置viewmodel属性 if (bevm.IsOK == true) { _batteryService.SuperAdd(editItem); } }
private void SaveAs() { Battery bc = new Battery(); //实例化一个新的model BatteryEditViewModel bevm = new BatteryEditViewModel(bc, _batteryTypeServie.Items); //实例化一个新的view model bevm.Name = _selectedItem.Name; bevm.BatteryType = bevm.AllBatteryTypes.SingleOrDefault(i => i.Id == _selectedItem.BatteryType.Id); bevm.CycleCount = _selectedItem.CycleCount; bevm.AssetUseCount = _selectedItem.AssetUseCount; bevm.DisplayName = "Battery-Save As"; bevm.commandType = CommandType.SaveAs; var BatteryViewInstance = new BatteryView(); //实例化一个新的view BatteryViewInstance.DataContext = bevm; BatteryViewInstance.ShowDialog(); if (bevm.IsOK == true) { _batteryService.SuperAdd(bc); } }
private void Edit() { Battery editItem = new Battery(); //实例化一个新的model BatteryEditViewModel bevm = new BatteryEditViewModel(editItem, _batteryTypeServie.Items); //实例化一个新的view model bevm.Id = _selectedItem.Id; bevm.Name = _selectedItem.Name; //bevm.BatteryType = _selectedItem.BatteryType; //不能用这种方式,猜是因为传来传去之后,_selectedItem.BatteryType已经不是bevm.AllBatteryTypes的一员了 bevm.BatteryType = bevm.AllBatteryTypes.SingleOrDefault(i => i.Id == _selectedItem.BatteryType.Id); //所以改用Id来找到List里的item bevm.CycleCount = _selectedItem.CycleCount; bevm.AssetUseCount = _selectedItem.AssetUseCount; bevm.DisplayName = "Battery-Edit"; bevm.commandType = CommandType.Edit; var BatteryViewInstance = new BatteryView(); //实例化一个新的view BatteryViewInstance.DataContext = bevm; BatteryViewInstance.ShowDialog(); if (bevm.IsOK == true) { _batteryService.SuperUpdate(editItem); } }