开发者

How to add arrow to each WPF listview column to indicate sort direction

I have a gridview ...with multiple columns. If I click on one column, it sorts the gridview based on that column values. I like to add an arrow in that "clicked" column header(when user clicks) to show that "this particular column is clicked and this is the sort direction"

How can I do this?

I saw a sample link How to I display a sort arrow in the header of a list view column using C#?

but its uses Windows.Forms.开发者_运维问答ListView..but i am using Windows.Controls.Listview :(


I used this tutorial to solve the same problem you have: http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-2-sorting

It contains a solution by using a custom Adorner, which is added to the ListView-Header.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Media;
using System.Windows.Documents;

namespace ListViewTest3
{
  public partial class ListViewTest : Window
  {
    private ObservableCollection<GameData> _GameCollection =
        new ObservableCollection<GameData>();

    private GridViewColumnHeader _CurSortCol = null;
    private SortAdorner _CurAdorner = null;

    public ListViewTest()
    {
      _GameCollection.Add(new GameData {
          GameName = "World Of Warcraft",
          Creator = "Blizzard",
          Publisher = "Blizzard" });
      _GameCollection.Add(new GameData {
          GameName = "Halo",
          Creator = "Bungie",
          Publisher = "Microsoft" });
      _GameCollection.Add(new GameData {
          GameName = "Gears Of War",
          Creator = "Epic",
          Publisher = "Microsoft" });

      InitializeComponent();
    }

    public ObservableCollection<GameData> GameCollection
    { get { return _GameCollection; } }

    private void AddRowClick(object sender, RoutedEventArgs e)
    {
      _GameCollection.Add(new GameData {
          GameName = "A New Game",
          Creator = "A New Creator",
          Publisher = "A New Publisher" });
    }

    private void SortClick(object sender, RoutedEventArgs e)
    {
      GridViewColumnHeader column = sender as GridViewColumnHeader;
      String field = column.Tag as String;

      if (_CurSortCol != null)
      {
        AdornerLayer.GetAdornerLayer(_CurSortCol).Remove(_CurAdorner);
        gameListView.Items.SortDescriptions.Clear();
      }

      ListSortDirection newDir = ListSortDirection.Ascending;
      if (_CurSortCol == column && _CurAdorner.Direction == newDir)
        newDir = ListSortDirection.Descending;

      _CurSortCol = column;
      _CurAdorner = new SortAdorner(_CurSortCol, newDir);
      AdornerLayer.GetAdornerLayer(_CurSortCol).Add(_CurAdorner);
      gameListView.Items.SortDescriptions.Add(
          new SortDescription(field, newDir));
    }
  }

  public class GameData
  {
    public string GameName { get; set; }
    public string Creator { get; set; }
    public string Publisher { get; set; }
  }

  public class SortAdorner : Adorner
  {
    private readonly static Geometry _AscGeometry =
        Geometry.Parse("M 0,0 L 10,0 L 5,5 Z");
    private readonly static Geometry _DescGeometry =
        Geometry.Parse("M 0,5 L 10,5 L 5,0 Z");

    public ListSortDirection Direction { get; private set; }

    public SortAdorner(UIElement element, ListSortDirection dir)
      : base(element)
    { Direction = dir; }

    protected override void OnRender(DrawingContext drawingContext)
    {
      base.OnRender(drawingContext);

      if (AdornedElement.RenderSize.Width < 20)
        return;

      drawingContext.PushTransform(
          new TranslateTransform(
            AdornedElement.RenderSize.Width - 15,
            (AdornedElement.RenderSize.Height - 5) / 2));

      drawingContext.DrawGeometry(Brushes.Black, null,
          Direction == ListSortDirection.Ascending ?
            _AscGeometry : _DescGeometry);

      drawingContext.Pop();
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜