LongListSelector,在VB中使用自定义分组

时间:2013-09-09 21:12:06

标签: vb.net windows-phone-8 longlistselector grouped-table

所以我正在研究WP8应用程序,我正试图让我的LongListSelector显示自定义组。基本上我所拥有的是一个部分,其中将显示消息但是可以从Web应用程序中的两个不同位置生成消息,因此需要将它们分组到适当的类别(警报和BTR)。我尝试浏览MSDN上的示例代码,但因为它只在C#中我被卡在某些部分上,现在我的LLS只显示列表中的一个项目。我认为这与我没有使用示例中引用的AlphaKeyGroup有关(但我再也无法使用它,因为我没有按消息中的字符排序,而是传递了一个类型值来自Webservice)。下面是我的LLS和日期模板的XAML代码

<!--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>

这是我用来创建带有组值和日期的快速测试的VB代码(在实际工作版本中,将显示三段文本,类型值可以是整数或字符串,如果在这种情况下一个比另一个更容易使用)。

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 个答案:

答案 0 :(得分:0)

所以我能够使用自定义字符串键组来解决这个问题,让我选择我想用作键的字符串。如果其他人想要它,我会在下面发布它:

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