Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6637731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:20:58+00:00 2026-05-25T23:20:58+00:00

I have a ListBox which happily displays data using a code-behind MVVM object. However,

  • 0

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.

Original xaml extract:

<ListBox SelectedItem="{Binding SelectedCategory}"
    DisplayMemberPath="name"
    ItemsSource="{Binding Categories}"
    Name="CategoriesListBox" />

Code behind extract:

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

Replacement XAML for ListBox:

<ListBox SelectedItem="{Binding SelectedCategory}"
    DisplayMemberPath="name"
    ItemsSource="{Binding Source={StaticResource SortedItems}}"
    Name="CategoriesListBox" />

And the CollectionViewSource:

<CollectionViewSource x:Key="SortedItems" Source="{Binding Categories}">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="name"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:20:58+00:00Added an answer on May 25, 2026 at 11:20 pm

    Use your original xaml

    <ListBox SelectedItem="{Binding SelectedCategory}" 
        DisplayMemberPath="name" 
        ItemsSource="{Binding Categories}" 
        Name="CategoriesListBox" /> 
    

    Update your View Model to use a List instead:

       public List<trainCategory> _categories;
       public List<trainCategory> Categories 
        { 
            get 
            {   // This LINQ statement returns a sorted list
                return (from c in _categories
                        orderby c
                        select c);
            } 
            set 
            { 
                if (_categories == value) 
                { 
                    return; 
                } 
                _categories = value; 
                RaisePropertyChanged("Categories"); 
            } 
        } //etc 
    

    Then you can skip all that nastiness of trying to bind to a static resporce. Just bind strait to the property in your view model.

    Alternately, you can still use arrays as your backing variable in your viewmodel:

       public trainCategory[] _categories;
       public List<trainCategory> Categories 
        { 
            get 
            {   // This LINQ statement returns a sorted list
                return (from c in _categories
                        orderby c
                        select c).ToList();
            } 
            set 
            { 
                if (_categories == value.ToArray()) 
                { 
                    return; 
                } 
                _categories = value.ToArray();
                RaisePropertyChanged("Categories");
            } 
        } //etc 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox which displays items of variable height. I want to show
I have this ListBox which is bound to an ObservableCollection. Each object in the
I have a WPF listbox which displays messages. It contains an avatar on the
I have a listbox which populated from using a datatable. I have a Add
I have a listbox that in which every item is represented using a textbox.
I have a listbox which is databound to a collection of objects. I want
I have a ListBox which I am using to display the results of various
I have a ListBox which displays the collection of MyObjects. The collection is in
I have an asp:listbox which I populate from a database. I am however trying
I have a listbox to which I have bound the data context to an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.