Ejemplo n.º 1
0
        private static void OnListViewHeaderClick(object sender, RoutedEventArgs e)
        {
            ListView             listView = e.Source as ListView;
            GridViewColumnHeader header   = e.OriginalSource as GridViewColumnHeader;

            if (header == null || header.Role == GridViewColumnHeaderRole.Padding)
            {
                return;
            }

            SortInfo sortInfo = listView.GetValue(SortInfoProperty.DependencyProperty) as SortInfo;

            if (sortInfo != null)
            {
                AdornerLayer.GetAdornerLayer(sortInfo.LastSortColumn).Remove(sortInfo.CurrentAdorner);
                listView.Items.SortDescriptions.Clear();
            }
            else
            {
                sortInfo = new SortInfo();
            }

            if (sortInfo.LastSortColumn == header)
            {
                (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection = (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
            }
            else
            {
                sortInfo.CurrentAdorner = new VisualAdorner(header, new ListSortDecorator());
            }

            sortInfo.LastSortColumn = header;
            listView.SetValue(SortInfoProperty, sortInfo);

            AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner);
            SortDescription sortDescriptioin = new SortDescription()
            {
                Direction    = (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection,
                PropertyName = header.Column.GetValue(SortFieldProperty) as string ?? header.Column.Header as string
            };

            listView.Items.SortDescriptions.Add(sortDescriptioin);
        }
Ejemplo n.º 2
0
        private static void InitializeSortDirection(ListView listView)
        {
            #region Guards
            var dataSource = listView.ItemsSource;
            if (dataSource == null)
            {
                return;
            }

            var dataview = CollectionViewSource.GetDefaultView(dataSource);
            if (dataview == null || dataview.SortDescriptions.Count == 0)
            {
                return;
            }

            SortInfo sortInfo = listView.GetValue(SortInfoProperty.DependencyProperty) as SortInfo;
            if (sortInfo != null)
            {
                return;
            }
            #endregion

            foreach (var columnHeader in listView.FindChildren <GridViewColumnHeader>(h => h.Role != GridViewColumnHeaderRole.Padding))
            {
                if (columnHeader.Column != null && dataview.SortDescriptions.Count > 0 &&
                    dataview.SortDescriptions[0].PropertyName.Equals(GetSortField(columnHeader.Column)))
                {
                    sortInfo = new SortInfo();
                    sortInfo.LastSortColumn = columnHeader;
                    sortInfo.CurrentAdorner = new VisualAdorner(columnHeader, new ListSortDecorator());
                    AdornerLayer.GetAdornerLayer(columnHeader).Add(sortInfo.CurrentAdorner);
                    listView.SetValue(SortInfoProperty, sortInfo);
                    break;
                }
            }
        }
Ejemplo n.º 3
0
 internal static void SetSortInfo(DependencyObject obj, SortInfo value)
 {
     obj.SetValue(SortInfoProperty.DependencyProperty, value);
 }
        private static void InitializeSortDirection(ListView listView)
        {
            #region Guards
            var dataSource = listView.ItemsSource;
            if (dataSource == null)
                return;

            var dataview = CollectionViewSource.GetDefaultView(dataSource);
            if (dataview == null || dataview.SortDescriptions.Count == 0)
                return;

            SortInfo sortInfo = listView.GetValue(SortInfoProperty.DependencyProperty) as SortInfo;
            if (sortInfo != null)
                return;
            #endregion

            foreach (var columnHeader in listView.FindChildren<GridViewColumnHeader>(h => h.Role != GridViewColumnHeaderRole.Padding))
            {
                if (columnHeader.Column != null && dataview.SortDescriptions.Count > 0 &&
                    dataview.SortDescriptions[0].PropertyName.Equals(GetSortField(columnHeader.Column)))
                {
                    sortInfo = new SortInfo();
                    sortInfo.LastSortColumn = columnHeader;
                    sortInfo.CurrentAdorner = new VisualAdorner(columnHeader, new ListSortDecorator());
                    AdornerLayer.GetAdornerLayer(columnHeader).Add(sortInfo.CurrentAdorner);
                    listView.SetValue(SortInfoProperty, sortInfo);
                    break;
                }
            }
        }
 internal static void SetSortInfo(DependencyObject obj, SortInfo value)
 {
     obj.SetValue(SortInfoProperty.DependencyProperty, value);
 }
        private static void OnListViewHeaderClick(object sender, RoutedEventArgs e)
        {
            ListView listView = e.Source as ListView;
            GridViewColumnHeader header = e.OriginalSource as GridViewColumnHeader;
            if (header == null || header.Role == GridViewColumnHeaderRole.Padding)
            {
                return;
            }

            SortInfo sortInfo = listView.GetValue(SortInfoProperty.DependencyProperty) as SortInfo;

            if (sortInfo != null)
            {
                AdornerLayer.GetAdornerLayer(sortInfo.LastSortColumn).Remove(sortInfo.CurrentAdorner);
                listView.Items.SortDescriptions.Clear();
            }
            else
                sortInfo = new SortInfo();

            if (sortInfo.LastSortColumn == header)
                (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection = (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
            else
                sortInfo.CurrentAdorner = new VisualAdorner(header, new ListSortDecorator());

            sortInfo.LastSortColumn = header;
            listView.SetValue(SortInfoProperty, sortInfo);

            AdornerLayer.GetAdornerLayer(header).Add(sortInfo.CurrentAdorner);
            SortDescription sortDescriptioin = new SortDescription()
            {
                Direction = (sortInfo.CurrentAdorner.Child as ListSortDecorator).SortDirection,
                PropertyName = header.Column.GetValue(SortFieldProperty) as string ?? header.Column.Header as string
            };
            listView.Items.SortDescriptions.Add(sortDescriptioin);
        }