/// <summary> /// Retrieve the IPanelCollection associated with the menu ID if it is already created; otherwise, create it and then return it. /// </summary> /// <param name="menuItemId">ID of the selected menu item.</param> /// <returns>IPanelCollection for the selected menu item.</returns> private IPanelCollection GetPanelCollectionForMenuItemId(String menuItemId) { IPanelCollection returnValue = null; Type panelCollectionType = null; // Translate the MenuItem ID into an IPanelCollection Type if (menuItemId.Equals(Variables.MENU_ITEM_ID_HOME)) { panelCollectionType = typeof(HomePanelCollection); } else if (menuItemId.Equals(Variables.MENU_ITEM_ID_TROUBLE_CODES)) { panelCollectionType = typeof(TroubleCodePanelCollection); } else if (menuItemId.Equals(Variables.MENU_ITEM_ID_CONFIGURATION)) { panelCollectionType = typeof(ConfigurationPanelCollection); } // If this type does not already exist in the panel collections Dictionary, // create a new instance and add it. If it does, return that instance. if (!(_panelCollections.ContainsKey(menuItemId))) { returnValue = (IPanelCollection)Activator.CreateInstance(panelCollectionType); _panelCollections.Add(menuItemId, returnValue); } else { _panelCollections.TryGetValue(menuItemId, out returnValue); } return(returnValue); }
/// <summary> /// Re-render the panels displayed from the selected panel collection. /// </summary> private void UpdatePanelsFromCollection(IPanelCollection panelCollection) { GridSplitter gridSplitter; System.Windows.Controls.MenuItem menuItem; // Prepare the Panels for Removal if (_selectedPanelCollection != null) { foreach (IRegisteredPanel nextPanel in _selectedPanelCollection.Panels) { // Notify them to Pause Monitoring nextPanel.PauseMonitoring(); // Unregister this MainWindow's event handlers from the Panel's events nextPanel.UnRegisterEventHandler(Variables.REGISTERED_EVENT_TYPE_HIDE_PANEL, RemovePanelFromDisplay); nextPanel.UnRegisterEventHandler(Variables.REGISTERED_EVENT_TYPE_SHOW_PANEL, AddPanelToDisplay); } } // Change Collections _selectedPanelCollection = panelCollection; // Clear out the Content Area ContentAreaGrid.Children.Clear(); ContentAreaGrid.RowDefinitions.Clear(); // Remove all menu items menAvailablePanels.Items.Clear(); // Build the Row Definitions for the Home Panels and Add the panels foreach (IRegisteredPanel nextPanel in _selectedPanelCollection.Panels) { // Add the panel to available panels menu menuItem = new System.Windows.Controls.MenuItem(); menuItem.Header = new Label() { Content = nextPanel.Title, Padding = new Thickness(0.0d) }; menuItem.IsCheckable = true; menuItem.IsChecked = nextPanel.IsShown; // Register the panel's event handlers to the MenuItem's events menuItem.Checked += nextPanel.ShowPanel; menuItem.Unchecked += nextPanel.HidePanel; // Register this MainWindow's event handlers to the Panel's events nextPanel.RegisterEventHandler(Variables.REGISTERED_EVENT_TYPE_HIDE_PANEL, RemovePanelFromDisplay); nextPanel.RegisterEventHandler(Variables.REGISTERED_EVENT_TYPE_SHOW_PANEL, AddPanelToDisplay); menAvailablePanels.Items.Add(menuItem); // Add the Panel to be displayed if (nextPanel.IsShown) { // Add the next panel ((UserControl)nextPanel).SetValue(Grid.RowProperty, ContentAreaGrid.RowDefinitions.Count); ((UserControl)nextPanel).SetValue(Grid.ColumnProperty, 0); ContentAreaGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); ContentAreaGrid.Children.Add((UserControl)nextPanel); // Unpause the next panel if (nextPanel.IsPaused) { nextPanel.UnPauseMonitoring(); } else { nextPanel.StartMonitoring(ELM327Connection.Connection); } // Add a GridSplitter gridSplitter = new GridSplitter() { Height = 4.0d, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 3, 0, 3), Background = new SolidColorBrush(Colors.Transparent) }; gridSplitter.SetValue(Grid.RowProperty, ContentAreaGrid.RowDefinitions.Count); gridSplitter.SetValue(Grid.ColumnProperty, 0); ContentAreaGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto }); ContentAreaGrid.Children.Add(gridSplitter); } } }