/// <summary> /// Initializes a new instance of the AdvancedSearchViewModel class /// </summary> public AdvancedSearchViewModel() { scope = Data.GraphManager.Instance.DefaultGraphComponentsInstance.Scope; // Setup a listener for the AttributeListUpdated event Data.Attributes.GlobalAttributeCollection.GetInstance(this.scope).AttributeListUpdated += new EventHandler <Data.Attributes.AttributeEventArgs>(GlobalAttributeCollection_AttributeListUpdated); // Get a list of all the attributes Attributes = new ObservableCollection <string>(Data.Attributes.GlobalAttributeCollection.GetInstance(scope).GetAttributes().Where(attribute => attribute.Visible).Select(attribute => attribute.Name)); // Get a list of all valid operators Operators = new ObservableCollection <string>(ExtensionMethods.GetNames(typeof(SearchOperator))); // Make sure that we have one criterion and that it is disabled // and inactive. SearchCriterionViewModel newSearchCriterion = new SearchCriterionViewModel(this) { IsEnabled = true, IsActive = false }; SearchCriteria.Add(newSearchCriterion); // Wire up event handlers for the Activated and Deactivated events of // the newly created SearchCriterion control newSearchCriterion.Activated += new EventHandler <EventArgs>(SearchCriterion_Activated); newSearchCriterion.Deactivated += new EventHandler <EventArgs>(SearchCriterion_Deactivated); }
/// <summary> /// Handles the Deactivated event for any SearchCriterion control. This event /// is fired when a SearchControl is made inactive, which means it should be /// removed. /// </summary> /// <param name="sender">The specific SearchCriterion instance that fired the event</param> /// <param name="e">Event arguments for the event</param> private void SearchCriterion_Deactivated(object sender, EventArgs e) { SearchCriterionViewModel criterion = sender as SearchCriterionViewModel; // Check if we have more than one SearchCriterion controls if (SearchCriteria.Count > 1) { // Remove the SearchCriterion from the collection SearchCriteria.Remove(criterion); // Remove event handlers criterion.Activated -= new EventHandler <EventArgs>(SearchCriterion_Activated); criterion.Deactivated -= new EventHandler <EventArgs>(SearchCriterion_Deactivated); } else { // Set the IsActive property to false which will trigger the // underlying StoryBoard to disable the input controls. criterion.IsActive = false; } }
/// <summary> /// Handles the Activated event for any SearchCriterion control. This event /// is fired when a SearchCriterion is made active, which mean that the input /// controls should be activated and a new (inactive) control should be created. /// However, if a control is not valid, a new control can't be added. /// </summary> /// <param name="sender">The specific SearchCriterion instance that fired the event</param> /// <param name="e">Event arguments for the event</param> private void SearchCriterion_Activated(object sender, EventArgs e) { bool addNew = true; SearchCriterionViewModel sourceCriterion = sender as SearchCriterionViewModel; // Check how many SearchCriterion controls we currently have if (SearchCriteria.Count > 1) { // Loop through all SearchCriterion view models in our list foreach (SearchCriterionViewModel criterion in SearchCriteria) { // If this criterion control is the one that fired the Activate // event, just skip it if (criterion == sourceCriterion) { continue; } // Check if the SearchCriterion is currently active if (criterion.IsActive) { // Validate the Search Criterion criterion.Validate(); if (!criterion.IsValid) { // Set a flag that we don't actually want to create // a new Criterion because this one was invalid. addNew = false; SelectedCriterion = criterion; } } } } // Check if we are actually adding a new SearchCritrion control if (addNew) { // Set the IsActive property to true which will trigger the // underlying StoryBoard to enable the input controls. sourceCriterion.IsActive = true; // Make sure that we have one criterion and that it is disabled // and inactive. SearchCriterionViewModel newSearchCriterion = new SearchCriterionViewModel(this) { IsEnabled = true, IsActive = false }; SearchCriteria.Add(newSearchCriterion); SelectedCriterion = sourceCriterion; // Wire up event handlers for the Activated and Deactivated events of // the newly created SearchCriterion control newSearchCriterion.Activated += new EventHandler <EventArgs>(SearchCriterion_Activated); newSearchCriterion.Deactivated += new EventHandler <EventArgs>(SearchCriterion_Deactivated); } else { sourceCriterion.IsActive = false; } }