Apply Custom Sorting Logic to a Collection : ObservableCollection « Windows Presentation Foundation « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Windows Presentation Foundation » ObservableCollectionScreenshots 
Apply Custom Sorting Logic to a Collection
Apply Custom Sorting Logic to a Collection
  

<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"
    Title="WPF" Height="300" Width="180">

    <Window.Resources>
        <local:SortableCountries x:Key="sortableCountries"/>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <ItemsControl ItemsSource="{StaticResource sortableCountries}" />
            <Button Click="SortButton_Click" Content="Sort" Margin="8" />
        </StackPanel>

    </Grid>

</Window>

//File:Window.xaml.cs

using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Collections.ObjectModel;

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

        private void SortButton_Click(object sender, RoutedEventArgs args)
        {
            SortableCountries sortableCountries = (SortableCountries)(this.Resources["sortableCountries"]);

            ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(sortableCountries);

            lcv.CustomSort = new SortCountries();
        }
    }

    public class SortCountries : IComparer
    {
        public int Compare(object x, object y)
        {
            string stringX = x.ToString();
            string stringY = y.ToString();
            
            return stringX.CompareTo(stringY);

        }
    }

    public class SortableCountries : ObservableCollection<string>
    {
        public SortableCountries()
        {
            this.Add("C");
            this.Add("B");
            this.Add("A");
        }
    }
}

   
    
  
Related examples in the same category
1.Use ObservableCollection as ResourceUse ObservableCollection as Resource
2.Bind to ObservableCollectionBind to ObservableCollection
3.Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).Create a ListView control that uses a GridView view mode to display the contents of an ObservableCollection<(Of <(T>)>).
4.Bind to ObservableCollection and ItemsSourceBind to ObservableCollection and ItemsSource
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.