Enables sorting of data in ascending or descending order according to the contents of one column. : ListView « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » ListView 
24.34.15.Enables sorting of data in ascending or descending order according to the contents of one column.
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="ListViewSort.Window1"    
    xmlns:s="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:p="clr-namespace:System;assembly=mscorlib">
  <Window.Resources>
    <DataTemplate x:Key="HeaderTemplateArrowUp">
      <DockPanel>
        <TextBlock HorizontalAlignment="Center" Text="{Binding}"/>
        <Path x:Name="arrow"
           StrokeThickness = "1"            
           Fill            = "gray"
           Data            = "M 5,10 L 15,10 L 10,5 L 5,10"/>
     </DockPanel>
    </DataTemplate>
    <DataTemplate x:Key="HeaderTemplateArrowDown">
      <DockPanel>
        <TextBlock HorizontalAlignment="Center" Text="{Binding }"/>
        <Path x:Name="arrow"
              StrokeThickness = "1"            
              Fill            = "gray"
              Data            = "M 5,5 L 10,10 L 15,5 L 5,5"/>
      </DockPanel>
    </DataTemplate>
  </Window.Resources>
  
  <StackPanel>
    <ListView x:Name='lv' Height="150" GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
      <ListView.View>
        <GridView>
          <GridViewColumn DisplayMemberBinding="{Binding Path=Year}" 
                          Header="Year"
                          Width="100"/>
        </GridView>
      </ListView.View>
      <ListView.ItemsSource>
        <s:ArrayList>
          <p:DateTime>2003/1/1 12:22:02</p:DateTime>
          <p:DateTime>2003/1/2 13:2:01</p:DateTime>
          <p:DateTime>2007/1/3 2:1:6</p:DateTime>
          <p:DateTime>2007/1/4 13:6:55</p:DateTime>
          <p:DateTime>2009/2/1 12:22:02</p:DateTime>
          <p:DateTime>2008/2/2 13:2:01</p:DateTime>
          <p:DateTime>2000/2/3 2:1:6</p:DateTime>
          <p:DateTime>2002/2/4 13:6:55</p:DateTime>
          <p:DateTime>2001/3/1 12:22:02</p:DateTime>
          <p:DateTime>2006/3/2 13:2:01</p:DateTime>
          <p:DateTime>2004/3/3 2:1:6</p:DateTime>
          <p:DateTime>2004/3/4 13:6:55</p:DateTime>
        </s:ArrayList>
      </ListView.ItemsSource>
    </ListView>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using System.Windows.Data;

namespace ListViewSort
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        GridViewColumnHeader _lastHeaderClicked = null;
        ListSortDirection _lastDirection = ListSortDirection.Ascending;

        void GridViewColumnHeaderClickedHandler(object sender,RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;

            if (headerClicked == null){
               return;
            }
            if (headerClicked.Role != GridViewColumnHeaderRole.Padding){
               return;
            }
            if (headerClicked != _lastHeaderClicked){
               direction = ListSortDirection.Ascending;
            }else{
               if (_lastDirection == ListSortDirection.Ascending){
                  direction = ListSortDirection.Descending;
               }else{
                  direction = ListSortDirection.Ascending;
               }
            }

            string header = headerClicked.Column.Header as string;
            Sort(header, direction);

            if (direction == ListSortDirection.Ascending)
            {
               headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowUp"as DataTemplate;
            }else{
               headerClicked.Column.HeaderTemplate = Resources["HeaderTemplateArrowDown"as DataTemplate;
            }
            if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
            {
               _lastHeaderClicked.Column.HeaderTemplate = null;
            }
            _lastHeaderClicked = headerClicked;
            _lastDirection = direction;
            
        }
        private void Sort(string sortBy, ListSortDirection direction)
        {
            ICollectionView dataView = CollectionViewSource.GetDefaultView(lv.ItemsSource);

            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }
    }
}
WPF Enables Sorting Of Data In Ascending Or Descending Order According To The Contents Of One Column
24.34.ListView
24.34.1.Create a ListView control that implements a GridView view with CheckBox controls for each row.Create a ListView control that implements a GridView view with CheckBox controls for each row.
24.34.2.Use ArrayList as the ListView ItemSourceUse ArrayList as the ListView ItemSource
24.34.3.Create a ListView control that implements a GridView view mode, displays content in groups.Create a ListView control that implements a GridView view mode, displays content in groups.
24.34.4.ListView and ListViewItemListView and ListViewItem
24.34.5.ListView columnsListView columns
24.34.6.Populating ListView rowsPopulating ListView rows
24.34.7.Set Binding ListView.ItemsSourceProperty to ListViewSet Binding ListView.ItemsSourceProperty to ListView
24.34.8.Create a ListView control that uses a GridView view mode to display a collection of DateTime objects.Create a ListView control that uses a GridView view mode to display a collection of DateTime objects.
24.34.9.ListView using GridView.HeaderTemplate and GridViewColumn.CellTemplate propertiesListView using GridView.HeaderTemplate and GridViewColumn.CellTemplate properties
24.34.10.Get Bounded item from ListViewGet Bounded item from ListView
24.34.11.Create Binding for ListView in codeCreate Binding for ListView in code
24.34.12.Use three TextBlocks in one ListViewItemUse three TextBlocks in one ListViewItem
24.34.13.Create a ListView control that uses a GridView view mode to display dates.Create a ListView control that uses a GridView view mode to display dates.
24.34.14.Use Path to reference Bounded object in ItemSourceUse Path to reference Bounded object in ItemSource
24.34.15.Enables sorting of data in ascending or descending order according to the contents of one column.Enables sorting of data in ascending or descending order according to the contents of one column.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.