Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get the CollectionView that is defined in XAML
    primarykey
    data
    text
    <p>I wanted to bind to an <code>ObservableCollection</code> in XAML and also apply the grouping there. In principle, this worked fine.</p> <pre><code>&lt;UserControl.Resources&gt; &lt;CollectionViewSource x:Key="cvs" Source="{Binding Path=TestTemplates}"&gt; &lt;CollectionViewSource.SortDescriptions&gt; &lt;scm:SortDescription PropertyName="Title"/&gt; &lt;/CollectionViewSource.SortDescriptions&gt; &lt;CollectionViewSource.GroupDescriptions&gt; &lt;PropertyGroupDescription PropertyName="TestCategory"/&gt; &lt;/CollectionViewSource.GroupDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;/UserControl.Resources&gt; </code></pre> <p>Then the data binding expression became <code>ItemsSource="{Binding Source={StaticResource ResourceKey=cvs}}"</code> instead of <code>ItemsSource="{Binding Path=TestTemplates}"</code>.</p> <p>At first, everything seemed cool, until I wanted to refresh the UI from the view model. The problem is, that <code>CollectionViewSource.GetDefaultView(TestTemplates)</code> returned a different view than the one from XAML where the grouping was applied. Thus, I could not set selection or do anything useful with it.</p> <p>I could fix it by binding the list again directly to the view model's property and setting up the grouping in the code-behind. But I'm not that happy with this solution.</p> <pre><code>private void UserControlLoaded(object sender, RoutedEventArgs e) { IEnumerable source = TemplateList.ItemsSource; var cvs = (CollectionView)CollectionViewSource.GetDefaultView(source); if (cvs != null) { cvs.SortDescriptions.Add(new SortDescription("Title", ListSortDirection.Ascending)); cvs.GroupDescriptions.Add(new PropertyGroupDescription("TestCategory")); } } </code></pre> <p>I assume, the reason for that is already <a href="https://stackoverflow.com/questions/5489004/" title="Why does CollectionViewSource.GetDefaultView() return the wrong CurrentItem from inside a Task thread?">given by John Skeet here</a>.</p> <p>Nevertheless, I would expect that there should be a way to get the right view. Am I wrong?</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.
 

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