Пример #1
0
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            Uri uri = new Uri("/Controls/FiterListViewDictionary.xaml", UriKind.Relative);

            dictionary = Application.LoadComponent(uri) as ResourceDictionary;

            // cast the ListView's View to a GridView
            GridView gridView = this.View as GridView;

            if (gridView != null)
            {
                // apply the data template, that includes the popup, button etc ... to each column
                foreach (GridViewColumn gridViewColumn in gridView.Columns)
                {
                    SortableGridViewColumn sc = gridViewColumn as SortableGridViewColumn;
                    if (sc != null && sc.CanBeFiltered)
                    {
                        gridViewColumn.HeaderTemplate = (DataTemplate)dictionary["FilterGridHeaderTemplate"];
                    }
                    else
                    {
                        gridViewColumn.HeaderTemplate = (DataTemplate)dictionary["SortableGridHeaderTemplate"];
                    }
                }
            }
        }
        ///
        /// Executes when the control is initialized completely the first time through. Runs only once.
        ///
        ///
        protected override void OnInitialized(EventArgs e)
        {
            Uri uri = new Uri("/Controls/FiterListViewDictionary.xaml", UriKind.Relative);

            dictionary = Application.LoadComponent(uri) as ResourceDictionary;


            // add the event handler to the GridViewColumnHeader. This strongly ties this ListView to a GridView.
            this.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(GridViewColumnHeaderClickedHandler));

            // 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)
                {
                    gridViewColumn.HeaderTemplate = (DataTemplate)dictionary["SortableGridHeaderTemplate"];

                    sortableGridViewColumn = gridViewColumn as SortableGridViewColumn;

                    if (sortableGridViewColumn != null)
                    {
                        if (sortableGridViewColumn.IsDefaultSortColumn)
                        {
                            break;
                        }
                        sortableGridViewColumn = null;
                    }
                }

                // if the default sort column is defined, sort the data and then update the templates as necessary.
                if (sortableGridViewColumn != null)
                {
                    lastSortedOnColumn = sortableGridViewColumn;
                    Sort(sortableGridViewColumn.SortPropertyName, ListSortDirection.Ascending);

                    if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate))
                    {
                        sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate;
                    }

                    this.SelectedIndex = 0;
                }
            }

            base.OnInitialized(e);
        }
Пример #3
0
        /// <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)Helpers.FindElementOfTypeUp(button, typeof(GridViewColumnHeader));

                // then down to the popup
                Popup popup = (Popup)Helpers.FindElementOfType(header, typeof(Popup));

                if (popup != null)
                {
                    SortableGridViewColumn column = (SortableGridViewColumn)header.Column;
                    String propertyName           = column.SortPropertyName;

                    var filterList = new List <FilterItem>();

                    var uniqueValues = new HashSet <object>();

                    if (IsPropertyFiltered(propertyName))
                    {
                        filterList.Add(new FilterItem("[clear]"));
                    }
                    else
                    {
                        bool containsNull = false;
                        PropertyDescriptor filterPropDesc = TypeDescriptor.GetProperties(ListItemType)[propertyName];

                        foreach (Object item in Items)
                        {
                            object value = filterPropDesc.GetValue(item);
                            if (value != null)
                            {
                                if (uniqueValues.Add(value))
                                {
                                    filterList.Add(new FilterItem(value as IComparable));
                                }
                            }
                            else
                            {
                                containsNull = true;
                            }
                        }

                        filterList.Sort();

                        if (containsNull)
                        {
                            filterList.Add(new FilterItem(null));
                        }
                    }

                    // open the popup to display this list
                    popup.DataContext = filterList;
                    CollectionViewSource.GetDefaultView(filterList).Refresh();
                    popup.IsOpen = true;

                    // connect to the selection change event
                    ListView listView = (ListView)popup.Child;
                    listView.SelectionChanged += SelectionChangedHandler;
                }
            }
        }
        ///
        /// Event Handler for the ColumnHeader Click Event.
        ///
        ///
        ///
        private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            Uri uri = new Uri("/Controls/FiterListViewDictionary.xaml", UriKind.Relative);
            ResourceDictionary dictionary = Application.LoadComponent(uri) as ResourceDictionary;

            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)
            {
                // attempt to cast to the sortableGridViewColumn object.
                SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;

                // ensure that the column header is the correct type and a sort property has been set.
                if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
                {
                    ListSortDirection?direction;

                    // determine if this is a new sort, or a switch in sort direction.
                    if (lastSortedOnColumn == null ||
                        String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName) ||
                        !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        direction = ListSortDirection.Ascending;
                    }
                    else
                    {
                        if (lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }
                        else if (lastDirection == ListSortDirection.Descending)
                        {
                            direction = null;
                        }
                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }


                    // get the sort property name from the column's information.
                    string sortPropertyName = sortableGridViewColumn.SortPropertyName;

                    // Sort the data.
                    if (direction == null)
                    {
                        RemoveSort();
                    }
                    else
                    {
                        Sort(sortPropertyName, direction.Value);
                    }

                    Label sortIndicator = (Label)Helpers.SingleFindDownInTree(headerClicked,
                                                                              new Helpers.FinderMatchName("sortIndicator"));

                    if (direction == null)
                    {
                        sortIndicator.Style = (Style)dictionary["HeaderTemplateTransparent"];
                    }
                    if (direction == ListSortDirection.Ascending)
                    {
                        sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowUp"];
                    }
                    else if (direction == ListSortDirection.Descending)
                    {
                        sortIndicator.Style = (Style)dictionary["HeaderTemplateArrowDown"];
                    }

                    // Remove arrow from previously sorted header
                    if (lastSortedOnColumnHeader != null && lastSortedOnColumnHeader != headerClicked)
                    {
                        sortIndicator = (Label)Helpers.SingleFindDownInTree(lastSortedOnColumnHeader,
                                                                            new Helpers.FinderMatchName("sortIndicator"));

                        sortIndicator.Style = (Style)dictionary["HeaderTemplateTransparent"];
                    }

                    if (direction == null)
                    {
                        lastSortedOnColumn       = null;
                        lastSortedOnColumnHeader = null;
                    }
                    else
                    {
                        lastSortedOnColumn       = sortableGridViewColumn;
                        lastSortedOnColumnHeader = headerClicked;
                    }
                }
            }
        }