LongListSelector with custom Grouping in VB

475 views Asked by At

So I'm working on WP8 app and I'm trying to figure out to get my LongListSelector to show custom groups. Basically what I have is a section where messages will be displayed but messages can be generated from 2 different places in the web application so they need to be grouped in the appropriate categories (Alerts and BTR's). I tried going through the example code that is on MSDN but since it's only in C# I'm getting stuck on certain parts and right now my LLS is only showing one item from the list. I think it has something to do with me not using the AlphaKeyGroup that is referenced in the example (but again I can't really use it since I'm not sorting by a character in the messages but instead there's an type value that is passed from the Webservice). Below is my XAML code for the LLS and the Date Templates

<!--Panorama item one-->
            <phone:PanoramaItem x:Name="item1" Header="Messages">
                <phone:LongListSelector x:Name="messageList" 
                                        LayoutMode="List"
                                        GroupHeaderTemplate="{StaticResource MessagesHeaderTemplate}"
                                        ItemTemplate="{StaticResource AlertMessageTemplate}" IsGroupingEnabled="True"/>
            </phone:PanoramaItem>

<!-- Template for Alert Messages-->
        <DataTemplate x:Key="AlertMessageTemplate">
            <StackPanel VerticalAlignment="Top">
                <TextBlock Text="{Binding AlertDate}" Foreground="Black"/>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="MessagesHeaderTemplate">
            <Border Background="Transparent" Padding="5">
                <Border Background="#FF27AAE1" Margin="0,0,18,0" HorizontalAlignment="Left">
                    <TextBlock Text="{Binding MessageGroup}"  Foreground="White" FontSize="48" Padding="6" 
            FontFamily="{StaticResource PhoneFontFamilySemiLight}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                </Border>
            </Border>
        </DataTemplate>

And here is the VB code that I'm using just to create a quick test with a Group value and a date (in the actual working version there will be three pieces of text that are displayed and the type value can be an integer or string if one is easier than the other to work with in this scenario).

Private Sub SetupMessageList()
        Dim source As New List(Of AlertMessage)()
        source.Add(New AlertMessage("Alert", "10/01/2010"))
        source.Add(New AlertMessage("Alert", "11/01/2010"))
        source.Add(New AlertMessage("Alert", "2/15/2012"))
        source.Add(New AlertMessage("Alert", "3/15/2013"))
        source.Add(New AlertMessage("BTR", "10/01/2010"))
        source.Add(New AlertMessage("BTR", "11/01/2010"))
        source.Add(New AlertMessage("BTR", "2/15/2012"))
        source.Add(New AlertMessage("BTR", "3/15/2013"))
        messageList.ItemsSource = source
    End Sub
    'Class For building Alert Messages from Web Service
    Public Class AlertMessage
        Private m_MessageGroup As String
        Public Property MessageGroup As String
            Get
                Return m_MessageGroup
            End Get
            Set(value As String)
                m_MessageGroup = value
            End Set
        End Property

        Private m_AlertDate As String
        Public Property AlertDate As String
            Get
                Return m_AlertDate
            End Get
            Set(value As String)
                m_AlertDate = value
            End Set
        End Property

        Public Sub New(messagegroup As String, alertdate As String)
            Me.MessageGroup = messagegroup
            Me.AlertDate = alertdate
        End Sub
    End Class
1

There are 1 answers

0
Sal Aldana On BEST ANSWER

So I was able to figure it out using a custom string key group that would let me choose what string I want to use as a Key. I'm posting it below in case anyone else wants it:

Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Globalization
Imports System.Linq

Public Class StringKeyGroup(Of T)
    Inherits ObservableCollection(Of T)
    Public Delegate Function GetKeyDelegate(item As T) As String
    Public Property Key() As String
        Get
            Return m_Key
        End Get
        Private Set(value As String)
            m_Key = value
        End Set
    End Property
    Private m_Key As String
    Public Sub New(key__1 As String)
        Key = key__1
    End Sub
    Public Shared Function CreateGroups(items As IEnumerable(Of T), ci As CultureInfo, getKey As GetKeyDelegate, sort As Boolean) As ObservableCollection(Of StringKeyGroup(Of T))
        Dim list = New ObservableCollection(Of StringKeyGroup(Of T))()

        For Each itemstring In items
            Dim index = -1
            For i As Integer = 0 To list.Count - 1
                If list(i).Key.Equals(getKey(itemstring)) Then
                    index = i
                    Exit For
                End If
            Next
            If index = -1 Then
                list.Add(New StringKeyGroup(Of T)(getKey(itemstring)))
                index = list.Count - 1
            End If
            If index >= 0 AndAlso index < list.Count Then
                list(index).Add(itemstring)
            End If
        Next

        If sort Then
            For Each group In list
                group.ToList().Sort(Function(c0, c1) ci.CompareInfo.Compare(getKey(c0), getKey(c1)))
            Next
        End If

        Return list
    End Function
End Class