// Constructor public MainWindow() { // Set the data context of the window to the MainWindowViewModel class DataContext = View.Model.MainWindow; // Populate initial data in the user interface's food list List <string> l = USDA.GetFoodList("", true); MessageBox.Show(l.Count.ToString()); View.Model.MainWindow.FoodList = USDA.GetFoodList("", true); // Populate the tree view List <string> list = USDA.GetFoodGroups(); foreach (var group in list) { CategorizedFoodListItem entry = new CategorizedFoodListItem(group, USDA.GetFoodListByGroup(group)); View.Model.MainWindow.CategorizedFoodList.Add(entry); } // Load user data UserData data = UserData.Load(); if (data != null) { View.Model.MainWindow.UserData = data; } // Load the user interface InitializeComponent(); // Set name scopes for context menus so they can bind to elements in the XAML tree NameScope.SetNameScope(DietPlanDataGridContextMenu, NameScope.GetNameScope(this)); }
private void UpdateTotals(string scope) { FoodData totals = new FoodData(); if (View.Model.MainWindow.UserData.PlannedDiet == null) { return; } foreach (DietPlanEntry entry in View.Model.MainWindow.UserData.PlannedDiet) { // Restrict results to the specified scope if (entry.Scope != scope) { continue; } FoodData data = USDA.GetFoodData(entry.FoodName); // Adjust the nutrient values to reflect the quantity and unit double quantity = Convert.ToDouble(entry.Quantity); string NDB_No = USDA.GetNDBNumber(entry.FoodName); double factor = USDA.GetNutrientConversionFactor(NDB_No, entry.Unit, quantity); data *= factor; // Add the adjusted data to the total totals += data; } // Apply rounding to the data if the option is set and update the data if (Settings.Default.Rounding == true) { totals = FoodData.Round(totals, Settings.Default.RoundingNumberOfFractionalDigits); } View.Model.MainWindow.FoodData = totals; }
// The user has completed a drop over the (diet plan) datagrid control with the source as either the listbox or the treeview private void DataGrid_Drop(object sender, DragEventArgs e) { DataObject item = (((DragEventArgs)e).Data) as DataObject; string food = item.GetData(DataFormats.StringFormat) as string; DataGrid grid = (DataGrid)e.Source; // Set the units to display in the combobox for the new row in the datagrid List <string> units = new List <string>(); units.Add("Gram"); List <string> foodunits = USDA.GetUnits(USDA.GetNDBNumber(food)); foreach (string unit in foodunits) { units.Add(unit.FirstLetterToUpper()); } foodunits = null; // Create a new DietEntry object and set values DietPlanEntry d = new DietPlanEntry(); d.FoodName = food; d.Units = units; d.Quantity = "100"; d.Unit = "gram"; d.Meal = "Breakfast"; d.Scope = ((ListBoxItem)DietPlanScopeListBox.SelectedItem).Content.ToString(); // Add the new DietEntry object to the diet plan tab View.Model.MainWindow.UserData.PlannedDiet.Add(d); // Save user data View.Model.MainWindow.UserData.Save(); e.Handled = true; }
public void Execute(object parameter) { if (View.Model.MainWindow.TotalsTabDataStatus.UpdateNeeded == false && View.Model.MainWindow.TotalsTabDataStatus.DataChanged == false) { return; } // Flag that helps this command fire at the proper time which is set to true initially and set to false as needed View.Model.MainWindow.TotalsTabDataStatus.IsShowingDataGridTotals = true; string _parameter = ((string)parameter).ToLower(); switch (_parameter) { // Passing one of the following strings causes appropriate data to be displayed in the Totals tab case "daily": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Daily' meal plan"; UpdateTotals("Daily"); break; case "mondays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Mondays' meal plan"; UpdateTotals("Mondays"); break; case "tuesdays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Tuesdays' meal plan"; UpdateTotals("Tuesdays"); break; case "wednesdays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Wednesdays' meal plan"; UpdateTotals("Wednesdays"); break; case "thursdays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Thursdays' meal plan"; UpdateTotals("Thursdays"); break; case "fridays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Fridays' meal plan"; UpdateTotals("Fridays"); break; case "saturdays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Saturdays' meal plan"; UpdateTotals("Saturdays"); break; case "sundays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Sundays' meal plan"; UpdateTotals("Sundays"); break; case "weekends": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Weekends' meal plan"; UpdateTotals("Weekends"); break; case "weekdays": View.Model.MainWindow.TotalsTabDescription = "Totals for the 'Weekdays' meal plan"; UpdateTotals("Weekdays"); break; default: // Display details for an individual food item (the case when one of the above keywords is not passed in _parameter // Update the Totals tab description text View.Model.MainWindow.TotalsTabDescription = "Totals for: '" + _parameter + "' (100 gram portion)"; // Apply rounding to the data if the option is set and update the data FoodData d = USDA.GetFoodData(_parameter); if (Properties.Settings.Default.Rounding) { d = FoodData.Round(d, Settings.Default.RoundingNumberOfFractionalDigits); } View.Model.MainWindow.FoodData = d; View.Model.MainWindow.TotalsTabDataStatus.IsShowingDataGridTotals = false; break; } if (View.Model.MainWindow.TotalsTabDataStatus.DataChanged) { View.Model.MainWindow.UserData.Save(); } View.Model.MainWindow.TotalsTabDataStatus.DataChanged = false; View.Model.MainWindow.TotalsTabDataStatus.UpdateNeeded = false; }
public void Execute(object parameter) { // Delegate to the USDA.GetFoodList method, passing both the search string as the first argument and the search mode based on radio selection View.Model.MainWindow.FoodList = USDA.GetFoodList(parameter.ToString(), View.Model.MainWindow.RadioButtonStartsWith); }