A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
Hello @RogerSchlueter-7899 ,
Thanks for your question.
Since your CollectionViewSource has no Source, the sorter does not know what to sort. Furthermore, if your ListBox binds directly to the raw collection instead of going through the CollectionViewSource, the sorting rules are completely bypassed.
You can refer to my example code:
MainWindow.xaml
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="Test Storyboard" Height="912" Width="1908">
<Window.Resources>
<CollectionViewSource
x:Key="cvsStandards"
Source="{Binding Standards}"
IsLiveSortingRequested="True">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="Entry" />
</CollectionViewSource.SortDescriptions>
<CollectionViewSource.LiveSortingProperties>
<sys:String>Entry</sys:String>
</CollectionViewSource.LiveSortingProperties>
</CollectionViewSource>
</Window.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource cvsStandards}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Entry, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Window>
In MainWindow.xaml.vb
Imports System.Collections.ObjectModel
Public Class MainWindow
Public Property Standards As ObservableCollection(Of Standard)
Public Sub New()
InitializeComponent()
Standards = New ObservableCollection(Of Standard)()
Standards.Add(New Standard() With {.Entry = "Zebra"})
Standards.Add(New Standard() With {.Entry = "Apple"})
Standards.Add(New Standard() With {.Entry = "Mango"})
Standards.Add(New Standard() With {.Entry = "Banana"})
Standards.Add(New Standard() With {.Entry = "Cherry"})
DataContext = Me
End Sub
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim cvs As CollectionViewSource =
TryCast(Me.Resources("cvsStandards"), CollectionViewSource)
If cvs IsNot Nothing Then
Dim view As ListCollectionView =
TryCast(cvs.View, ListCollectionView)
If view IsNot Nothing Then
view.LiveSortingProperties.Add("Entry")
view.IsLiveSorting = True
End If
End If
End Sub
End Class
In Standard.vb
Imports System.ComponentModel
Public Class Standard
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
Private _Entry As String
Public Property Entry As String
Get
Return _Entry
End Get
Set(value As String)
If value <> _Entry Then
_Entry = value
OnPropertyChanged("Entry")
End If
End Set
End Property
Private Sub OnPropertyChanged(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
I tested and it automatically re-sorts in real time as you type inside any TextBox.
I hope this addresses your question. If this response was helpful, please consider following the guidance to provide feedback.