private void FilterColumn(GridViewColumnHeader header) { GridViewColumnWithFilterAndSorter column = (GridViewColumnWithFilterAndSorter)header.Column; String currentFilterProperty = column.SortPropertyName; bool hasFilter = false; if (column.FilterItems == null) { if (currentFilters.ContainsKey(currentFilterProperty)) { currentFilters.Remove(currentFilterProperty); } } else { foreach (var filterItem in column.FilterItems) { if (filterItem.IsChecked) { if (!currentFilters.ContainsKey(currentFilterProperty)) { currentFilters[currentFilterProperty] = new List <object>(); } currentFilters[currentFilterProperty].Add(filterItem.ItemView); hasFilter = true; } else { if (currentFilters.ContainsKey(currentFilterProperty)) { currentFilters[currentFilterProperty].Remove(filterItem.ItemView); } } } } // find the button and apply the active style Button button = (Button)BaseWPFHelpers.FindVisualElement(header, "filterButton"); if (hasFilter) { button.ContentTemplate = (DataTemplate)dictionary["filterButtonActiveTemplate"]; if (FilterButtonActiveStyle != null) { button.Style = FilterButtonActiveStyle; } } else { currentFilters.Remove(currentFilterProperty); button.ContentTemplate = (DataTemplate)dictionary["filterButtonInactiveTemplate"]; if (FilterButtonActiveStyle != null) { button.Style = FilterButtonActiveStyle; } } ApplyCurrentFilters(); }
void popup_Closed(object sender, EventArgs e) { Popup popup = (Popup)sender; // navigate up to the header to obtain the filter property name GridViewColumnHeader header = (GridViewColumnHeader)BaseWPFHelpers.FindElementOfTypeUp(popup, typeof(GridViewColumnHeader)); FilterColumn(header); popup.Closed -= new EventHandler(popup_Closed); }
private void RefreshItemsCommand(object sender, ExecutedRoutedEventArgs e) { GridViewColumnHeader header = (GridViewColumnHeader)BaseWPFHelpers.FindElementOfTypeUp((MenuItem)e.OriginalSource, typeof(GridViewColumnHeader)); Popup popup = (Popup)BaseWPFHelpers.FindElementOfType(header, typeof(Popup)); GridViewColumnWithFilterAndSorter column = (GridViewColumnWithFilterAndSorter)header.Column; ReBuildFiltersList(column.SortPropertyName, (ObservableCollection <FilterItem>)popup.DataContext); //popup.IsOpen = false; }
/// /// Event Handler for the ColumnHeader Click Event. /// /// /// private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e) { GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader; // ensure that we clicked on the column header and not the padding that's added to fill the space. if (headerClicked != null && headerClicked.Role != GridViewColumnHeaderRole.Padding) { SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn; // attempt to cast to the sortableGridViewColumn object. var dataView = CollectionViewSource.GetDefaultView(this.ItemsSource); // ensure that the column header is the correct type and a sort property has been set. if (sortableGridViewColumn != null) { // get the sort property name from the column's information. string sortPropertyName = sortableGridViewColumn.SortPropertyName; if (!String.IsNullOrEmpty(sortPropertyName)) { var lastDirection = sortableGridViewColumn.SortDirection; ListSortDirection direction = SortableGridViewColumn.NoSort; // determine if this is a new sort, or a switch in sort direction. if (lastDirection == ListSortDirection.Ascending) { direction = ListSortDirection.Descending; } else if (lastDirection == ListSortDirection.Descending) { direction = SortableGridViewColumn.NoSort; } else { direction = ListSortDirection.Ascending; } Label sortIndicator = (Label)BaseWPFHelpers.SingleFindDownInTree(headerClicked, new BaseWPFHelpers.FinderMatchName("sortIndicator")); // Sort the data. Sort(sortPropertyName, direction); if (direction == SortableGridViewColumn.NoSort) { sortIndicator.Style = (Style)dictionary["HeaderTemplateTransparent"]; } else { if (direction == ListSortDirection.Ascending) { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowUp"]; } else { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowDown"]; } } sortableGridViewColumn.SortDirection = direction; } } } }
private void RemoveFiltersCommand(object sender, ExecutedRoutedEventArgs e) { GridViewColumnHeader header = (GridViewColumnHeader)BaseWPFHelpers.FindElementOfTypeUp((MenuItem)e.OriginalSource, typeof(GridViewColumnHeader)); GridViewColumnWithFilterAndSorter column = (GridViewColumnWithFilterAndSorter)header.Column; Popup popup = (Popup)BaseWPFHelpers.FindElementOfType(header, typeof(Popup)); //foreach (var filterItem in column.FilterItems) //{ // filterItem.IsChecked = false; //} column.FilterItems = null; popup.IsOpen = false; }
public override void OnApplyTemplate() { base.OnApplyTemplate(); // ensure that the custom inactive style is applied if (FilterButtonInactiveStyle != null) { List <FrameworkElement> columnHeaders = BaseWPFHelpers.FindElementsOfType(this, typeof(GridViewColumnHeader)); foreach (FrameworkElement columnHeader in columnHeaders) { Button button = (Button)BaseWPFHelpers.FindElementOfType(columnHeader, typeof(Button)); if (button != null) { button.Style = FilterButtonInactiveStyle; } } } }
/// <summary> /// Handles the ShowFilter command to populate the filter list and display the popup /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ShowFilterCommand(object sender, ExecutedRoutedEventArgs e) { Button button = e.OriginalSource as Button; if (button != null) { // navigate up to the header GridViewColumnHeader header = (GridViewColumnHeader)BaseWPFHelpers.FindElementOfTypeUp(button, typeof(GridViewColumnHeader)); // then down to the popup Popup popup = (Popup)BaseWPFHelpers.FindElementOfType(header, typeof(Popup)); if (popup != null) { // find the property name that we are filtering GridViewColumnWithFilterAndSorter column = GetColumnFilters(header); // open the popup to display this list popup.Closed += new EventHandler(popup_Closed); popup.DataContext = column.FilterItems; popup.IsOpen = true; } } }
void SortableListView_Loaded(object sender, RoutedEventArgs e) { FindTheHeaders(); // cast the ListView's View to a GridView GridView gridView = this.View as GridView; if (gridView != null) { // determine which column is marked as IsDefaultSortColumn. Stops on the first column marked this way. SortableGridViewColumn sortableGridViewColumn = null; foreach (GridViewColumn gridViewColumn in gridView.Columns) { sortableGridViewColumn = gridViewColumn as SortableGridViewColumn; if (sortableGridViewColumn != null) { if (sortableGridViewColumn.SortDirection != SortableGridViewColumn.NoSort && !string.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName)) { var dataView = CollectionViewSource.GetDefaultView(this.ItemsSource); if (dataView != null) { dataView.SortDescriptions.Add(new SortDescription(sortableGridViewColumn.SortPropertyName, sortableGridViewColumn.SortDirection)); GridViewColumnHeader header = m_Headers[sortableGridViewColumn]; if (header != null) { ListSortDirection direction = sortableGridViewColumn.SortDirection; Label sortIndicator = (Label)BaseWPFHelpers.SingleFindDownInTree(header, new BaseWPFHelpers.FinderMatchName("sortIndicator")); if (direction == SortableGridViewColumn.NoSort) { sortIndicator.Style = (Style)dictionary["HeaderTemplateTransparent"]; } else { if (direction == ListSortDirection.Ascending) { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowUp"]; } else { sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowDown"]; } } } //if (sortableGridViewColumn.SortDirection == ListSortDirection.Ascending) //{ // if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate)) // { // sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate; // } //} //else //{ // if (!String.IsNullOrEmpty(this.ColumnHeaderSortedDescendingTemplate)) // { // sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedDescendingTemplate) as DataTemplate; // } //} } } } } } }