// Toggle the sort order (ascending/descending) for the field selected in the sort fields list private void ChangeFieldSortOrder(object sender, RoutedEventArgs e) { // Verify that there is a selected sort field in the list OrderFieldOption selectedSortField = OrderByFieldsListBox.SelectedItem as OrderFieldOption; if (selectedSortField == null) { return; } // Create a new order field info to define the sort for the selected field OrderBy newOrderBy = new OrderBy(selectedSortField.OrderInfo.FieldName, selectedSortField.OrderInfo.SortOrder); OrderFieldOption newSortDefinition = new OrderFieldOption(true, newOrderBy); // Toggle the sort order from the current value if (newSortDefinition.OrderInfo.SortOrder == SortOrder.Ascending) { newSortDefinition.OrderInfo.SortOrder = SortOrder.Descending; } else { newSortDefinition.OrderInfo.SortOrder = SortOrder.Ascending; } // Add the new OrderBy at the same location in the collection and remove the old one _orderByFields.Insert(_orderByFields.IndexOf(selectedSortField), newSortDefinition); _orderByFields.Remove(selectedSortField); }
// Add the selected field in the "group by" list to the "order by" list private void AddSortFieldClicked(object sender, RoutedEventArgs e) { // Verify that there is a selected field in the "group by" list if (GroupFieldsListBox.SelectedItem == null) { return; } // Get the name of the selected field and ensure that it's in the list of selected group fields (checked on in the list, e.g.) string selectedFieldName = GroupFieldsListBox.SelectedItem.ToString(); if (!_groupByFields.Contains(selectedFieldName)) { ShowMessage("Only fields used for grouping can be used to order results.", "Order fields"); return; } // Verify that the field isn't already in the "order by" list OrderFieldOption existingOrderBy = _orderByFields.FirstOrDefault(f => f.OrderInfo.FieldName == selectedFieldName); if (existingOrderBy == null) { // Create a new OrderBy for this field and add it to the collection (default to ascending sort order) OrderBy newOrderBy = new OrderBy(selectedFieldName, SortOrder.Ascending); OrderFieldOption orderField = new OrderFieldOption(false, newOrderBy); _orderByFields.Add(orderField); } }
// Remove the selected field from the list of "order by" fields private void RemoveSortFieldClicked(object sender, RoutedEventArgs e) { // Verify that there is a selected item in the "order by" list if (OrderByFieldsListBox.SelectedItem == null) { return; } // Get the selected OrderFieldOption object and remove it from the collection OrderFieldOption selectedOrderBy = OrderByFieldsListBox.SelectedItem as OrderFieldOption; _orderByFields.Remove(selectedOrderBy); }
// Handle when the check box for a "group by" field is checked on or off by adding or removing the field from the collection private void GroupFieldCheckChanged(object sender, RoutedEventArgs e) { // Get the check box that raised the event (group field) CheckBox groupFieldCheckBox = (CheckBox)sender; // Get the field name string fieldName = groupFieldCheckBox.Content.ToString(); // See if the field is being added or removed from the "group by" list bool fieldAdded = groupFieldCheckBox.IsChecked == true; // See if the field already exists in the "group by" list bool fieldIsInList = _groupByFields.Contains(fieldName); // If the field is being added, and is NOT in the list, add it ... if (fieldAdded && !fieldIsInList) { _groupByFields.Add(fieldName); // Also add it to the "order by" list OrderBy orderBy = new OrderBy(fieldName, SortOrder.Ascending); OrderFieldOption orderOption = new OrderFieldOption(false, orderBy); _orderByFields.Add(orderOption); } // If the field is being removed and it IS in the list, remove it ... else if (!fieldAdded && fieldIsInList) { _groupByFields.Remove(fieldName); // Also check for this field in the "order by" list and remove if necessary (only group fields can be used to order results) OrderFieldOption orderBy = _orderByFields.FirstOrDefault(field => field.OrderInfo.FieldName == fieldName); if (orderBy != null) { // Remove the field from the "order by" list _orderByFields.Remove(orderBy); } } }