如何在LongListSelector中检索选定的组?

时间:2014-05-29 21:14:57

标签: c# windows-phone-8 longlistselector

我使用LongListSelector显示Group Contact Contact一个Group可以属于多个class Group { public string Name { get; set; } } class Contact { public string Name { get; set; } public List<Group> Groups { get; set; } } s

ItemsSource

我使用下面的代码为LongListSelector

构建public List<KeyedList<Group, Contact>> GroupedContacts { get { List<Group> groups = ...; List<Contact> contacts = ...; List<KeyedList<Group, Contact>> result = new List<KeyedList<Group, Contact>>(); foreach (Group gr0up in groups) { var temp = from c in contacts where c.Groups.Contains(gr0up) select c; List<Contact> groupedContacts = new List<Contact>(temp); result.Add(new KeyedList<Group, Contact>(gr0up, groupedContacts)); } return result; } }
SelectionChanged

从上面的代码中可以看出,单个联系人对象可以在几个组中使用。 我处理Contact事件并且我可以获得选定的Group,但我无法获得有关所选组的信息。

是否有任何'标准'可能性,<Grid> <Grid.Resources> <DataTemplate x:Key="GroupHeader"> <Grid Margin="3,3" Width="480" Height="90" HorizontalAlignment="Stretch" Hold="GroupDelete" Tag="{Binding Key.Name}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="{Binding Key.Name}"/> </Grid> </DataTemplate> <DataTemplate x:Key="ItemTemplate"> <StackPanel Height="128" Width="128" Orientation="Vertical"> <Grid> <Image Width="102" Height="102" VerticalAlignment="Top" HorizontalAlignment="Left" Stretch="UniformToFill"> <Image.Source> <BitmapImage UriSource="/Assets/contact_template.png" CreateOptions="BackgroundCreation"/> </Image.Source> </Image> <Image Width="36" Height="36" VerticalAlignment="Top" HorizontalAlignment="Right" Margin="10,5,35,0" Source="/Assets/delete_contact.png"/> </Grid> <TextBlock Text="{Binding Name}" Foreground="Black" VerticalAlignment="Top"/> </StackPanel> </DataTemplate> </Grid.Resources> <phone:LongListSelector Name="ContactsList" ItemsSource="{Binding GroupedContacts}" ItemTemplate="{StaticResource ItemTemplate}" GroupHeaderTemplate="{StaticResource GroupHeader}" Style="{StaticResource ContactsLongListSelectorStyle}" SelectionChanged="ContactsList_SelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled"/> </Grid> 已被选中?

更新

public class KeyedList<TKey, TItem> : List<TItem>
{
    public TKey Key { protected set; get; }

    public KeyedList(TKey key, IEnumerable<TItem> items)
        : base(items)
    {
        Key = key;
    }

    public KeyedList(IGrouping<TKey, TItem> grouping)
        : base(grouping)
    {
        Key = grouping.Key;
    }
}

KeyedList实现:

{{1}}

1 个答案:

答案 0 :(得分:1)

在ContactsList_SelectionChanged(sender,e)中:e.AddedItems [0]应该为您提供一个组/值对。

更新:要在SelectionChanged()中接收键/值对,LongListSelector的每个列表项都需要是键/值对。

这里是LongListSelector的演练(它的专长是组是字符串并根据值计算,但它可以很容易地变得更通用): http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365%28v=vs.105%29.aspx

所以示例的AddressBook类对你来说就像是

public class LongListGroupEntry
{
    public Group Key;
    public Contact Value;
}