Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting ListBox with MVVM code binding
    text
    copied!<p>I have a ListBox which happily displays data using a code-behind MVVM object. However, I want to sort the entries and so I thought an intermediate CollectionViewSource might work. But instead the program crashes on startup.</p> <p>Original xaml extract:</p> <pre><code>&lt;ListBox SelectedItem="{Binding SelectedCategory}" DisplayMemberPath="name" ItemsSource="{Binding Categories}" Name="CategoriesListBox" /&gt; </code></pre> <p>Code behind extract:</p> <pre><code>public class ViewModel : INotifyPropertyChanged { private trainCategory[] _categories; private trainCategory _selectedCategory; public event PropertyChangedEventHandler PropertyChanged; public trainCategory[] Categories { get { return _categories; } set { if (_categories == value) { return; } _categories = value; RaisePropertyChanged("Categories"); } } //etc </code></pre> <p>Replacement XAML for ListBox:</p> <pre><code>&lt;ListBox SelectedItem="{Binding SelectedCategory}" DisplayMemberPath="name" ItemsSource="{Binding Source={StaticResource SortedItems}}" Name="CategoriesListBox" /&gt; </code></pre> <p>And the CollectionViewSource:</p> <pre><code>&lt;CollectionViewSource x:Key="SortedItems" Source="{Binding Categories}"&gt; &lt;CollectionViewSource.SortDescriptions&gt; &lt;scm:SortDescription PropertyName="name"/&gt; &lt;/CollectionViewSource.SortDescriptions&gt; &lt;/CollectionViewSource&gt; </code></pre> <p>It seems to me that the CollectionViewSource goes in between the view model and the ListBox, but it clearly doesn't (or I've done it wrong). Any pointers appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload