public MainViewModel() { ActiveIndex = -1; Formula = new Formula(); // Add example components with arbitrary values var c = new ComponentItem(); c.Name = "Water"; c.Mass = 10.0; c.Density = 1.0; AddComponent(c); c = new ComponentItem(); c.Name = "Soap"; c.Mass = 4.21; c.Density = 1.73; AddComponent(c); c = new ComponentItem(); c.Name = "Bleach"; c.Mass = 0.24; c.Density = 0.92; AddComponent(c); //Formula.Calculate(); }
public void Calculate(ComponentItem sender) { try { if (IsCalculating) return; IsCalculating = true; // Initialize the variables TotalVolume = 0; TotalMass = 0; // Calculate the totals foreach (var item in Components) { if (!object.ReferenceEquals(sender, item)) item.Calculate(); TotalMass += item.Mass; TotalVolume += item.Volume; } // Calculate the component summaries foreach (var item in Components) { item.PercentByMass = (TotalMass == 0.0) ? 0.0 : item.Mass / TotalMass * 100.0; item.PercentByVolume = (TotalVolume == 0.0) ? 0.0 : item.Volume / TotalVolume * 100.0; } } finally { IsCalculating = false; } }
public void AddComponent(ComponentItem item) { if (!Components.Contains(item)) { item.Formula = this; Components.Add(item); item.Position = Components.IndexOf(item) + 1; item.Calculate(); Calculate(item); } }
private void UnsubscribeComponent(ComponentItem item) { PropertyChangedEventManager.RemoveHandler(item as INotifyPropertyChanged, HandlePropertyChanged, string.Empty); }
public bool RemoveComponent(ComponentItem item) { UnsubscribeComponent(item); bool result = Formula.RemoveComponent(item); RefreshSummaries(); return result; }
public ComponentItem AddComponent(ComponentItem item) { Formula.AddComponent(item); SubscribeComponent(item); RefreshSummaries(); return item; }
public void SetPosition(ComponentItem item, int position) { if (Components.Contains(item)) { // Check if the position is the same -- no change if (item.Position == position) return; --position; Components.Remove(item); Components.Insert(position, item); Renumber(); } }
public bool RemoveComponent(ComponentItem item) { bool result = Components.Remove(item); if (result) { Renumber(); Calculate(); } return result; }