public void ChangeDriver(DeviceViewModel deviceViewModel, DriverType newDriverType) { int maxAdress = (int)GetMaxAddress(); if (maxAdress - deviceViewModel.Device.Driver.Mult + Processor.DriversHelper.GetDriver(newDriverType).Mult > 255) { MessageBoxService.ShowWarning("Количество устройств на шлейфе не должно превышать 255"); return; } deviceViewModel.Device.DriverType = newDriverType; UpdateAddresses(Devices.IndexOf(deviceViewModel)); Calculate(); }
public LineViewModel(Line line) { Line = line; AddCommand = new RelayCommand(OnAdd, CanAdd); RemoveCommand = new RelayCommand<object>(OnRemove, CanRemove); CopyCommand = new RelayCommand<object>(OnCopy, CanCutCopy); CutCommand = new RelayCommand<object>(OnCut, CanCutCopy); PasteCommand = new RelayCommand(OnPaste, CanPaste); Devices = new ObservableCollection<DeviceViewModel>(); foreach (var device in line.Devices) { var deviceViewModel = new DeviceViewModel(device, this); Devices.Add(deviceViewModel); } SelectedDevice = Devices.FirstOrDefault(); UpdateAddresses(); Calculate(); }
void OnPaste() { var maxAddress = GetMaxAddress(); var newMaxAddress = maxAddress + DevicesToCopy.Sum(x => x.Driver.Mult); if (newMaxAddress > 255) { MessageBoxService.ShowWarning("Количество устройств на шлейфе не должно превышать 255"); return; } var firstIndex = Devices.IndexOf(SelectedDevice); foreach (var deviceToCopy in DevicesToCopy) { var selectedIndex = Devices.IndexOf(SelectedDevice); if (selectedIndex < 0) selectedIndex = 0; var device = new Device(); device.DriverType = deviceToCopy.DriverType; device.Cable.CableType = deviceToCopy.Cable.CableType; device.Cable.Resistivity = deviceToCopy.Cable.Resistivity; device.Cable.Length = deviceToCopy.Cable.Length; Line.Devices.Insert(selectedIndex, device); var deviceViewModel = new DeviceViewModel(device, this); Devices.Insert(selectedIndex, deviceViewModel); SelectedDevice = deviceViewModel; } UpdateAddresses(firstIndex - 1); Calculate(); }
void OnAdd() { var newDeviceViewModel = new NewDeviceViewModel(); if (DialogService.ShowModalWindow(newDeviceViewModel)) { var maxAddress = GetMaxAddress(); var driver = DriversHelper.GetDriver(newDeviceViewModel.SelectedDriver.DriverType); var newMaxAddress = maxAddress + driver.Mult * newDeviceViewModel.Count; if (newMaxAddress > 255) { MessageBoxService.ShowWarning("Количество устройств на шлейфе не должно превышать 255"); return; } var selectedIndex = Devices.IndexOf(SelectedDevice) + 1; DeviceViewModel deviceViewModel = null; for (int i = 0; i < newDeviceViewModel.Count; i++) { var device = new Device(); device.DriverType = newDeviceViewModel.SelectedDriver.DriverType; device.Cable.CableType = newDeviceViewModel.SelectedCableType; device.Cable.Resistivity = newDeviceViewModel.CableResistivity; device.Cable.Length = newDeviceViewModel.CableLength; Line.Devices.Insert(selectedIndex, device); deviceViewModel = new DeviceViewModel(device, this); Devices.Insert(selectedIndex, deviceViewModel); } SelectedDevice = deviceViewModel; UpdateAddresses(selectedIndex - 1); Calculate(); } }