Create Collection based Resource for data binding : ObservableCollection « Windows Presentation Foundation « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Windows Presentation Foundation » ObservableCollection 
16.104.2.Create Collection based Resource for data binding
<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.vb

Imports System
Imports System.Collections
Imports System.Windows
Imports System.Windows.Data
Imports System.Collections.ObjectModel

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()
    End Sub

    Private Sub SortButton_Click(sender As Object, args As RoutedEventArgs)
      Dim sortableCountries As SortableCountries = DirectCast(Me.Resources("sortableCountries"), SortableCountries)

      Dim lcv As ListCollectionView = DirectCast(CollectionViewSource.GetDefaultView(sortableCountries), ListCollectionView)

      lcv.CustomSort = New SortCountries()
    End Sub
  End Class

  Public Class SortCountries
    Implements IComparer
    Public Function Compare(As Object, y As ObjectAs Integer Implements IComparer.Compare
      Dim stringX As String = x.ToString()
      Dim stringY As String = y.ToString()

      Return stringX.CompareTo(stringY)

    End Function
  End Class

  Public Class SortableCountries
    Inherits ObservableCollection(Of String)
    Public Sub New()
      Me.Add("C")
      Me.Add("B")
      Me.Add("A")
    End Sub
  End Class
End Namespace
WPF Create Collection Based Resource For Data Binding
16.104.ObservableCollection
16.104.1.Bind to ObservableCollectionBind to ObservableCollection
16.104.2.Create Collection based Resource for data bindingCreate Collection based Resource for data binding
16.104.3.Use ObservableCollection as ResourceUse ObservableCollection as Resource
16.104.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.