Beispiel #1
0
        private static void OnListViewHeaderClick(object sender, RoutedEventArgs e)
        {
            ListView             listView = e.Source as ListView;
            GridViewColumnHeader header   = e.OriginalSource as GridViewColumnHeader;
            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 UIElementAdorner(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
            };

            //列的内容为图片时的错误处理
            try
            {
                listView.Items.SortDescriptions.Add(sortDescriptioin);
            }
            catch
            {
            }
        }
 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.Column != null)
            {
                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 UIElementAdorner(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);
            }
        }
 internal static void SetSortInfo(DependencyObject obj, SortInfo value)
 {
     obj.SetValue(SortInfoProperty.DependencyProperty, value);
 }