Create a ListView control that uses a GridView view mode to display dates.

image_pdfimage_print


   
  

<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=&#039;lv&#039; 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 &amp;&amp; _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();
        }
    }
}

   
    
     


Use Path to reference Bounded object in ItemSource

image_pdfimage_print


   
  




<Window x:Class="WpfApplication1.Monitor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=System"
    xmlns:debug="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    Title="Monitor" Height="400" Width="400">
    <Grid>
        <Grid.Resources>
            <ObjectDataProvider x:Key="processes" MethodName="GetProcesses" ObjectType="{x:Type diag:Process}"/>
        </Grid.Resources>
        <ListView Name="listView1" ItemsSource="{Binding Source={StaticResource processes}, debug:PresentationTraceSources.TraceLevel=High}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Path=Id}" MinWidth="80" />
                        <TextBlock Text="{Binding Path=ProcessName}" MinWidth="180" />
                        <TextBlock>
                            <TextBlock.Text>
                                <Binding Path="WorkingSet" />
                            </TextBlock.Text>
                        </TextBlock>
                    </WrapPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Monitor : Window
    {
        public Monitor()
        {
            InitializeComponent();
        }
    }
    public class AddPaddingValueConverter : IValueConverter
    {
        public AddPaddingValueConverter() { }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double d = System.Convert.ToDouble(value);
            return d + 20;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double d = System.Convert.ToDouble(value);
            return d - 20;
        }
    }    
}

   
    
     


Enables sorting of data in ascending or descending order according to the contents of one column.

image_pdfimage_print


   
  


<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=&#039;lv&#039; 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 &amp;&amp; _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();
        }
    }
}

   
    
     


Change the Appearance of Alternate Items in a List

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="248" Width="128">
    <Window.Resources>
        <local:Countries x:Key="countries"/>
        <Style x:Key="AlternateStyle">
            <Setter Property="ListBoxItem.Background" Value="LightGray"/>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource countries}}" >
            <ListBox.ItemContainerStyleSelector>
                <local:AlternatingRowStyleSelector AlternateStyle="{StaticResource AlternateStyle}" />
            </ListBox.ItemContainerStyleSelector>
        </ListBox>
    </Grid>
</Window>
//File:Window.xaml.cs
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
namespace WpfApplication1
{
    public class AlternatingRowStyleSelector : StyleSelector
    {
        public Style DefaultStyle
        {
            get;
            set;
        }

        public Style AlternateStyle
        {
            get;
            set;
        }

        private bool isAlternate = false;

        public override Style SelectStyle(object item, DependencyObject container)
        {
            Style style = isAlternate ? AlternateStyle : DefaultStyle;

            isAlternate = !isAlternate;

            return style;
        }
    }

    public class Country
    {
        private string name;
        private Continent continent;

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Continent Continent
        {
            get
            {
                return continent;
            }
            set
            {
                continent = value;
            }
        }
    }
    public enum Continent
    {
        Europe,
        NorthAmerica,
    }
    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }
    }
}

   
    
     


Change the Appearance of a List Item When It's Selected

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1;assembly="
    Title="WPF" Height="248" Width="128">
    <Window.Resources>

        <WpfApplication1:Countries x:Key="countries"/>

        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="Content" Value="{Binding Path=Name}"/>
            <Setter Property="Margin" Value="20"/>
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="FontSize" Value="14" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource countries}}"/>
    </Grid>

</Window>
//File:Window.xaml.cs
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    public class Country
    {
        private string name;
        private Continent continent;

        public Country(string name, Continent continent)
        {
            this.name = name;
            this.continent = continent;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Continent Continent
        {
            get
            {
                return continent;
            }
            set
            {
                continent = value;
            }
        }

    }

    public enum Continent
    {
        Europe,
        NorthAmerica,
    }

    public class Countries : Collection<Country>
    {
        public Countries()
        {
            this.Add(new Country("Great Britan", Continent.Europe));
            this.Add(new Country("USA", Continent.NorthAmerica));
            this.Add(new Country("Canada", Continent.NorthAmerica));
        }    
    }
}

   
    
     


Select Product Page Function

image_pdfimage_print


   
  
<PageFunction
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
    x:Class="NavigationApplication.SelectProductPageFunction"    
    Title="SelectProductPageFunction"
    xmlns:local="clr-namespace:NavigationApplication"
    x:TypeArguments="local:Product">
  <StackPanel>
    <ListBox Name="lstProducts">
      <ListBoxItem>A</ListBoxItem>
      <ListBoxItem>B</ListBoxItem>
      <ListBoxItem>C</ListBoxItem>
    </ListBox>

    
      <TextBlock Grid.Row="1">
        <Hyperlink Click="lnkOK_Click">OK</Hyperlink>
        <Hyperlink Click="lnkCancel_Click">Cancel</Hyperlink>
      </TextBlock>
    
  </StackPanel>
</PageFunction>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace NavigationApplication
{
    public partial class SelectProductPageFunction 
    {
        public SelectProductPageFunction()
        {
            InitializeComponent();
        }

        private void lnkOK_Click(object sender, RoutedEventArgs e)
        {
            ListBoxItem item = (ListBoxItem)lstProducts.SelectedItem;            
            Product product = new Product(item.Content.ToString());
            OnReturn(new ReturnEventArgs<Product>(product));
        }
        private void lnkCancel_Click(object sender, RoutedEventArgs e)
        {
            OnReturn(null);
        }
    }
    public class Product
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public Product(string name)
        {
            Name = name;
        }
    }    
}