Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I used this tutorial to solve the same problem you have: <a href="http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-2-sorting" rel="nofollow noreferrer">http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-2-sorting</a></p> <p>It contains a solution by using a custom Adorner, which is added to the ListView-Header.</p> <pre><code>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&lt;GameData&gt; _GameCollection = new ObservableCollection&lt;GameData&gt;(); 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&lt;GameData&gt; 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 &amp;&amp; _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 &lt; 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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload