0

所以我正在开发 WP8 应用程序,我试图让我的 LongListSelector 显示自定义组。基本上我所拥有的是一个显示消息的部分,但可以从 Web 应用程序中的 2 个不同位置生成消息,因此需要将它们分组到适当的类别(警报和 BTR)中。我尝试浏览 MSDN 上的示例代码,但由于它仅在 C# 中,我被困在某些部分,现在我的 LLS 只显示列表中的一项。我认为这与我不使用示例中引用的 AlphaKeyGroup 有关(但我也不能真正使用它,因为我没有按消息中的字符排序,而是传递了一个类型值来自网络服务)。下面是我的 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
4

1 回答 1

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
于 2013-10-25T21:34:13.730 回答