Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Introduction</h2> <p>Basically, if there is a need to display a sorted collection, please consider using the <code>CollectionViewSource</code> class: assign ("bind") its <code>Source</code> property to the source collection — an instance of the <code>ObservableCollection&lt;T&gt;</code> class.</p> <p>The idea is that <a href="http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource.getdefaultview%28v=vs.110%29.aspx" rel="nofollow noreferrer"><code>CollectionViewSource</code> class provides an instance of the <code>CollectionView</code> class</a>. This is kind of "projection" of the original (source) collection, but with applied sorting, filtering, etc.</p> <p>References:</p> <ul> <li><a href="https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-sort-and-group-data-using-a-view-in-xaml" rel="nofollow noreferrer">How to: Sort and Group Data Using a View in XAML</a>.</li> <li><a href="http://www.zagstudio.com/blog/387#" rel="nofollow noreferrer">WPF’s CollectionViewSource</a>.</li> </ul> <h3>Live Shaping</h3> <p>WPF 4.5 introduces "Live Shaping" feature for <code>CollectionViewSource</code>.</p> <p>References:</p> <ul> <li><a href="http://blogs.microsoft.co.il/pavely/2011/10/02/wpf-45-new-feature-live-shaping/" rel="nofollow noreferrer">WPF 4.5 New Feature: Live Shaping</a>.</li> <li><a href="https://docs.microsoft.com/en-us/dotnet/api/system.windows.data.collectionviewsource.islivesorting" rel="nofollow noreferrer">CollectionViewSource.IsLiveSorting Property</a>.</li> <li><a href="http://msdn.microsoft.com/en-us/library/bb613588%28v=vs.110%29.aspx#live_shaping" rel="nofollow noreferrer">Repositioning data as the data's values change (Live shaping)</a>.</li> </ul> <h2>Solution</h2> <p>If there still a need to sort an instance of the <code>ObservableCollection&lt;T&gt;</code> class, here is how it can be done. The <code>ObservableCollection&lt;T&gt;</code> class itself does not have sort method. But, the collection could be re-created to have items sorted:</p> <pre><code>// Animals property setter must raise "property changed" event to notify binding clients. // See INotifyPropertyChanged interface for details. Animals = new ObservableCollection&lt;string&gt; { "Cat", "Dog", "Bear", "Lion", "Mouse", "Horse", "Rat", "Elephant", "Kangaroo", "Lizard", "Snake", "Frog", "Fish", "Butterfly", "Human", "Cow", "Bumble Bee" }; ... Animals = new ObservableCollection&lt;string&gt;(Animals.OrderBy(i =&gt; i)); </code></pre> <h2>Additional details</h2> <p>Please note that <code>OrderBy()</code> and <code>OrderByDescending()</code> methods (as other LINQ–extension methods) <strong>do not modify</strong> the source collection! They instead <strong>create a new sequence</strong> (i.e. a new instance of the class that implements <code>IEnumerable&lt;T&gt;</code> interface). Thus, it is necessary to re-create the collection.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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