Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Assuming you're talking about the WPF Toolkit DataGrid control, you need only set <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.canusersortcolumns(VS.100,default).aspx" rel="noreferrer">the CanUserSortColumns property</a> to true and then set <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridcolumn.sortmemberpath(VS.100).aspx" rel="noreferrer">the SortMemberPath property</a> of each DataGridColumn in the DataGrid.</p> <p>As far as sorting the collection initially, you must use a CollectionViewSource and set the sort on that and then assign that as the ItemsSource of your DataGrid. If you're doing this in XAML then it would be as easy as:</p> <pre><code>&lt;Window.Resources&gt; &lt;CollectionViewSource x:Key="MyItemsViewSource" Source="{Binding MyItems}"&gt; &lt;CollectionViewSource.SortDescriptions&gt; &lt;scm:SortDescription PropertyName="MyPropertyName"/&gt; &lt;/CollectionViewSource.SortDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;/Window.Resources&gt; &lt;DataGrid ItemsSource="{StaticResource MyItemsViewSource}"&gt; &lt;/DataGrid&gt; </code></pre> <p><strong>NOTE:</strong> the "scm" namespace prefix maps to System.ComponentModel where the SortDescription class lives.</p> <pre><code>xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" </code></pre> <p><strong>EDIT:</strong> I think enough people got help from this post, that this upvoted comment should be included in this answer:</p> <p>I had to use this to get it to work:</p> <pre><code>&lt;DataGrid ItemsSource="{Binding Source={StaticResource MyItemsViewSource}}"&gt; </code></pre>
 

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