CAMOBAP
CAMOBAP

Reputation: 5657

How to retrieve the selected group in a LongListSelector?

I using LongListSelector for showing Groups with Contact (one Contact can belong to multiple Groups)

class Group
{
    public string Name { get; set; }
}

class Contact
{
    public string Name { get; set; }
    public List<Group> Groups { get; set; }
}

I use code below to build ItemsSource for 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;
    }
}

As you can see from the code above, single contact object can be used in several groups. I handle SelectionChanged event and I can get a selected Contact but I can't get information about the selected Group.

Is there any 'standard' possibility to know, which Group has been selected?

Update

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

KeyedList implementation:

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;
    }
}

Upvotes: 1

Views: 426

Answers (1)

HDW Production
HDW Production

Reputation: 1402

In ContactsList_SelectionChanged(sender,e): e.AddedItems[0] should give you a group/value pair.

UPDATE: To receive a key/value pair in SelectionChanged(), each list item of LongListSelector needs to be a key/value pair.

Here's a walkthrough for LongListSelector (It's specialized in that the groups are strings and calculated from the value, but it can easily be made more generic): http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365%28v=vs.105%29.aspx

So the example's AddressBook class would for you be something like

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

Upvotes: 1

Related Questions