Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper method of grouping a Collection in Windows Store App (Metro) using LINQ and VB.NET?
    primarykey
    data
    text
    <p>I'm a fairly novice programmer, and making a Windows Store App. I have an object that contains an <code>ObservableCollection(Of T)</code> that I want to group for display in a <code>ListView</code>. Using LINQ to return a set of grouped items seems like it would be the best and easiest choice. Every piece of documentation I could find says that Group By returns <code>IEnumerable(Of IGrouping(Of TKey, TItem))</code> and you set <code>CollectionViewSource.IsSourceGrouped</code> to True, bind to the CVS and it should all work perfectly. Right?</p> <p>Not so much.</p> <p>I have a class that contains</p> <pre><code>Public Property Items As ObservableCollection(Of Item) Public ReadOnly Property GroupedItems As IEnumerable Get ' Return grouped items End Get End Property </code></pre> <p>And in the XAML for the page, where <code>GroupedItems</code> is set as the <code>Page</code>'s <code>DataContext</code> in code-behind:</p> <pre><code>&lt;Page.Resources&gt; &lt;CollectionViewSource x:Name="GroupedItems" Source="{Binding}" IsSourceGrouped="True" /&gt; &lt;/Page.Resources&gt; &lt;Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"&gt; &lt;ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}"&gt; &lt;ListView.GroupStyle&gt; &lt;GroupStyle.HeaderTemplate&gt; &lt;DataTemplate&gt; &lt;!-- Template --&gt; &lt;/DataTemplate&gt; &lt;/GroupStyle.HeaderTemplate&gt; &lt;/ListView.GroupStyle&gt; &lt;ListView.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;!-- Template --&gt; &lt;/DataTemplate&gt; &lt;/ListView.ItemTemplate&gt; &lt;/ListView&gt; &lt;/Grid&gt; </code></pre> <p>Now, if I bind the <code>ListView</code> to the <code>ObservableCollection(Of Item)</code> directly, the <code>ListView</code> renders all the items as expected, following the <code>ItemTemplate</code>. But when it is bound to the supposedly-grouped <code>CollectionViewSource</code>, the <code>ListView</code> renders the groups according to the <code>HeaderTemplate</code>, but it doesn't render any of the items within the group.</p> <p>I've inspected the results of the LINQ logic:</p> <pre><code>Dim x = From i As Item In Items Group i By Key = i.SubItem Into Group </code></pre> <p>And <code>x</code> has a <code>Key</code> property and a <code>Group</code> property, though it is some kind of IEnumerable(Of anonymous) type rather than an <code>IEnumerable(Of IGrouping(Of TKey, TItem))</code>.</p> <p>So what am I doing wrong here? Is there another caveat to the VB.NET implementation of LINQ that I'm missing? Is there a property I'm forgetting to set? What is the right way to do this?</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. 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