private void CopyViewModelValuesToState(StateViewModel selectedViewModel, StateSave stateSave) { for (int i = 0; i < selectedViewModel.Variables.Count; i++) { ApplyViewModelVariableToStateAtIndex(selectedViewModel.Variables[i], i, selectedViewModel); } }
public StateCategoryViewModel(StateSaveCategory category, IElement element) { this.VariableManagementVisibility = Visibility.Collapsed; this.Element = element; this.category = category; Columns = new List <string>(); States = new ObservableCollection <StateViewModel>(); foreach (var state in category.States) { var stateViewModel = new StateViewModel(state, category, element); AssignEventsOn(stateViewModel); States.Add(stateViewModel); } CreateBlankViewModelAtEnd(element); IncludedVariables = new ObservableCollection <string>(); ExcludedVariables = new ObservableCollection <string>(); ExcludedVariables.CollectionChanged += HandleExcludedVariablesChanged; RefreshExcludedAndIncludedVariables(); this.Name = category.Name; this.PropertyChanged += HandlePropertyChanged; }
private void CreateBlankViewModelAtEnd(IElement element) { // add the blank one: var blankRowViewModel = new StateViewModel(null, category, element); AssignEventsOn(blankRowViewModel); States.Add(blankRowViewModel); }
private void HandleStateViewModelValueChanged(StateViewModel stateViewModel, StateVariableViewModel stateVariableViewModel) { var value = stateVariableViewModel.Value; var element = GlueState.Self.CurrentElement; var customVariable = element.GetCustomVariable(stateVariableViewModel.VariableName); ApplyValueToModel(value, stateViewModel, stateViewModel.BackingData, customVariable); }
public void ApplyViewModelVariableToStateAtIndex(object value, int index, StateViewModel stateViewModel) { if (index >= 0 && index < Element.CustomVariables.Count) { var stateSave = stateViewModel.BackingData; if (stateSave != null) { // If it's null, that means the user is editing the bottom-most row which doesn't yet have a state // This will be handled in the HandlePropertyChanged var variable = Element.CustomVariables[index]; // need to cast the type, because newItems[0] will be a string ApplyValueToModel(value, stateViewModel, stateSave, variable); } } }
private void CheckForStateRemoval(StateViewModel selectedState) { var isLast = selectedState == this.States.Last(); bool shouldRemove = false; if (!isLast) { var isEmpty = selectedState.Variables.All(item => item == null || (item is string && string.IsNullOrEmpty(item as string))); shouldRemove = isEmpty && string.IsNullOrEmpty(selectedState.Name); } if (shouldRemove) { this.category.States.Remove(selectedState.BackingData); int oldIndex = this.States.IndexOf(selectedState); this.States.RemoveAt(oldIndex); GlueCommands.Self.RefreshCommands.RefreshUi(this.category); GlueCommands.Self.GenerateCodeCommands.GenerateAllCodeTask(); GlueCommands.Self.GluxCommands.SaveGluxTask(); if (oldIndex > 0) { // Victor Chelaru June 8, 2018 // I can't get this to select, not sure why. // I've tried setting the selected state, the selected // index, and moved the code to different places in this method... // but the data grid always resets to 0. This seems to be a bug/ // peculiar behavior of the WPF datagrid according to StackOverflow posts //this.SelectedState = this.States[oldIndex - 1]; this.SelectedIndex = oldIndex - 1; } } }
private void ApplyViewModelVariableToStateAtIndex(object value, int index, StateViewModel stateViewModel) { if (index >= 0 && index < Element.CustomVariables.Count) { var stateSave = stateViewModel.BackingData; if (stateSave != null) { // If it's null, that means the user is editing the bottom-most row which doesn't yet have a state // This will be handled in the HandlePropertyChanged var variable = Element.CustomVariables[index]; // need to cast the type, because newItems[0] will be a string if (value == null || value as string == String.Empty) { var existingInstruction = stateSave.InstructionSaves.FirstOrDefault(item => item.Member == variable.Name); if (existingInstruction != null) { stateSave.InstructionSaves.Remove(existingInstruction); } CheckForStateRemoval(stateViewModel); } else { try { var convertedValue = Convert(value, variable); stateSave.SetValue(variable.Name, convertedValue); } catch (Exception e) { GlueCommands.Self.PrintError( $"Could not assign variable {variable.Name} ({variable.Type}) to \"{value}\":\n{e.ToString()}"); } } } } }
private void HandlePropertyOnStateChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(StateViewModel.Name)) { StateViewModel selectedViewModel = sender as StateViewModel; StateSave stateSave = selectedViewModel.BackingData; var needsToCreateNewState = selectedViewModel.BackingData == null && !string.IsNullOrEmpty(selectedViewModel.Name) && selectedViewModel.IsNameInvalid == false; if (needsToCreateNewState) { stateSave = new StateSave(); stateSave.Name = selectedViewModel.Name; var category = GlueState.Self.CurrentStateSaveCategory; category.States.Add(stateSave); selectedViewModel.BackingData = stateSave; // Add a new entry so the user can create more states easily: CreateBlankViewModelAtEnd(GlueState.Self.CurrentElement); CopyViewModelValuesToState(selectedViewModel, stateSave); } if (selectedViewModel.IsNameInvalid == false) { stateSave.Name = selectedViewModel.Name; GlueCommands.Self.GenerateCodeCommands.GenerateAllCodeTask(); GlueCommands.Self.GluxCommands.SaveGluxTask(); } GlueCommands.Self.RefreshCommands.RefreshUi(this.category); if (string.IsNullOrEmpty(selectedViewModel.Name)) { CheckForStateRemoval(selectedViewModel); } } }
public void ApplyValueToModel(object value, StateViewModel stateViewModel, StateSave stateSave, CustomVariable variable) { if (value == null || value as string == String.Empty) { var existingInstruction = stateSave?.InstructionSaves.FirstOrDefault(item => item.Member == variable.Name); if (existingInstruction != null) { stateSave.InstructionSaves.Remove(existingInstruction); } CheckForStateRemoval(stateViewModel); } else { try { var convertedValue = Convert(value, variable); // This could have converted incorrectly. if (convertedValue != null) { stateSave.SetValue(variable.Name, convertedValue); } else { var existingInstruction = stateSave?.InstructionSaves.FirstOrDefault(item => item.Member == variable.Name); if (existingInstruction != null) { stateSave.InstructionSaves.Remove(existingInstruction); } } } catch (Exception e) { GlueCommands.Self.PrintError( $"Could not assign variable {variable.Name} ({variable.Type}) to \"{value}\":\n{e.ToString()}"); } } }
private void AssignEventsOn(StateViewModel stateViewModel) { stateViewModel.PropertyChanged += HandlePropertyOnStateChanged; stateViewModel.Variables.CollectionChanged += HandleVariableCollectionOnStateChanged; }