<Window
x:Class="SortedList.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SortedList" Height="300" Width="521"
>
<DockPanel
LastChildFill="True">
<StackPanel
Orientation="Horizontal" DockPanel.Dock="Top">
<Button
Content="Use List" Click="UseList_Handler" Width="100"></Button>
<Button
Content="Use ListCollectionView" Click="UseCollectionView_Handler" Width="100"></Button>
<Button
Content="Use Observable" Click="UseObservable_Handler" Width="100"></Button>
<Button
Name="btnAdd" Click="btnClick_Handler">Button</Button>
<TextBox
Name="txtData" Width="100"></TextBox>
</StackPanel>
<ListBox
Margin="58,54,56,65" Name="lbData" />
</DockPanel>
</Window>
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.Shapes;
using
System.Diagnostics;
using
System.Collections.ObjectModel;
using
System.ComponentModel;
namespace
SortedList
{
/// <summary>
/// Interaction logic for
Window1.xaml
/// </summary>
public partial class Window1 : System.Windows.Window
{
private enum CollectionType {LIST, COLLECTIONVIEW,
OBSERVABLECOLLECTION}
private CollectionType
_eCT;
private List<string>
_lstData;
private ListCollectionView
_lstCV;
private ObservableCollection<string>
_lstOC;
public
Window1()
{
InitializeComponent();
}
private void UseList_Handler(object sender, RoutedEventArgs
args)
{
_eCT = CollectionType.LIST;
// Create
list
_lstData = new List<string>();
_lstData.Add("A");
_lstData.Add("E");
_lstData.Add("B");
_lstData.Add("D");
_lstData.Add("C");
// Sort
list
_lstData.Sort();
foreach (string s in
_lstData)
Trace.WriteLine(s);
// Now bind list to the list
box
lbData.ItemsSource = _lstData;
}
private void
UseCollectionView_Handler(object sender, RoutedEventArgs
args)
{
_eCT = CollectionType.COLLECTIONVIEW;
// Create
list
List<string> lstData = new List<string>();
lstData.Add("A");
lstData.Add("E");
lstData.Add("B");
lstData.Add("D");
lstData.Add("C");
_lstCV = new ListCollectionView(lstData); // Constructor must take a IList
list
// Sort
list
SortDescription sd =
new SortDescription();
sd.Direction = ListSortDirection.Ascending;
_lstCV.SortDescriptions.Add(sd);
// Now bind list to the list
box
lbData.ItemsSource = _lstCV;
}
private void UseObservable_Handler(object sender, RoutedEventArgs
args)
{
_eCT = CollectionType.OBSERVABLECOLLECTION;
// Create
list
_lstOC = new ObservableCollection<string>();
_lstOC.Add("A");
_lstOC.Add("E");
_lstOC.Add("B");
_lstOC.Add("D");
_lstOC.Add("C");
// Sort
list
ICollectionView view =
CollectionViewSource.GetDefaultView(_lstOC);
SortDescription sd =
new SortDescription();
sd.Direction = ListSortDirection.Ascending;
view.SortDescriptions.Add( sd);
// Now bind list to the list
box
lbData.ItemsSource = _lstOC;
}
// Adds items to the current data
source
private void btnClick_Handler(object sender, RoutedEventArgs
args)
{
switch
(_eCT)
{
case CollectionType.LIST:
// Data added to list but is not
reflected in the ListBox. To allow
changes
// to be reflected in the list box,
use an INotifyCollectionChanged-derived
container
_lstData.Add(txtData.Text);
break;
case CollectionType.COLLECTIONVIEW:
// CANNOT ADD ITEMS TO A
CollectionView-derived collection!
break;
case CollectionType.OBSERVABLECOLLECTION:
_lstOC.Add(txtData.Text);
break;
}
}
}
}