2
votes

I am using a multi-selection ListPicker (the new one in the 7.1/Mango control toolkit from Nov '11).

My code is below - a "vanilla" use case for the ListPicker, except that I initialize the SelecetedItems dependency property with a new List so I can add things to it and properly initialize the selected state for the ListPicker. Although this issue repro's whether or not I do this...

The SummaryForSelectedItemsDelegate does get called when initializing the list (e.g. when I call contactPicker.SetValue(ListPicker.SelectedItemsProperty)), but NOT when I click the "done" button on the ListPicker (although my SelectionChanged event handler does get called).

Once I dismiss the ListPicker, I only get the string corresponding to the first selected item in the "summary" for the control (as opposed to the control calling my delegate and getting a comma-delimited list of selected items).

Is this a bug? Has anyone else run into this? Is there a workaround?

var contactPicker = new ListPicker()
{
    MinWidth = minWidth,
    ExpansionMode = ExpansionMode.FullScreenOnly,
    SelectionMode = SelectionMode.Multiple,
    SummaryForSelectedItemsDelegate = (list) => { return CreateCommaDelimitedList(list); },
    IsTabStop = true
};

contactPicker.ItemsSource = listOfItems;
contactPicker.DisplayMemberPath = "Name";
contactPicker.SetValue(ListPicker.SelectedItemsProperty, new List<Item>());

// initialize the list picker selected values
foreach (var contactRef in listOfSelectedContacts)
    contactPicker.SelectedItems.Add(contactRef);

contactPicker.SelectionChanged += new SelectionChangedEventHandler((o, ea) => 
{
    // add all the newly added items
    foreach (var added in ea.AddedItems)
    {
        Item addedItem = added as Item;
        if (addedItem == null)
            continue;
        listOfSelectedContacts.Items.Add(addedItem);
    }

    // remove all the newly removed items
    foreach (var removed in ea.RemovedItems)
    {
        Item removedItem = removed as Item;
        if (removedItem == null)
            continue;
        listOfSelectedContacts.Items.Remove(removedItem);
    }
});
1

1 Answers

0
votes

I should have posted my my summary delegate... which is actually where my bug was :-(

Even though I was creating the SelectedItems as a List, and each of the elements in the IList passed in are typed "Item", the concrete type of the IList passed in is NOT List. Therefore the null check succeeds and the method returns null. And of course my breakpoint was right after that line so it looked like the method wasn't getting invoked. Duh.

    private string CreateCommaDelimitedList(IList ilist)
    {
        IList<Item> list = ilist as IList<Item>;
        if (list == null)
            return null;

        // build a comma-delimited list of names to display in a control
        List<string> names = list.Select(it => it.Name).ToList();
        StringBuilder sb = new StringBuilder();
        bool comma = false;
        foreach (var name in names)
        {
            if (comma)
                sb.Append(", ");
            else
                comma = true;
            sb.Append(name);
        }
        return sb.ToString();
    }