2012-05-27 13 views
2

我正在使用多選ListPicker(11月11日的7.1/Mango控件工具包中的新選項)。ListPicker在調用時不調用SummaryForSelectedItemsDelegate

我的代碼如下 - 一個ListFicker的「香草」用例,除了我用新的List初始化SelecetedItems依賴項屬性,所以我可以添加東西給它並正確初始化ListPicker的選定狀態。雖然這個問題repro的是否我這樣做...

SummaryForSelectedItemsDelegate在初始化列表時(例如當我調用contactPicker.SetValue(ListPicker.SelectedItemsProperty))時確實被調用,但不是當我單擊「完成」 ListPicker上的按鈕(雖然我的SelectionChanged事件處理程序確實被調用)。

一旦我關閉ListPicker,我只會得到與控件的「摘要」中的第一個選定項目相對應的字符串(與控制調用我的委託並獲取逗號分隔的所選項目列表相反)。

這是一個錯誤?有沒有其他人遇到過這個問題?有沒有解決方法?

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

回答

0

我應該已經張貼了我的我的總結代表......這實際上是我的錯誤是:-(

即使我創建的SelectedItems的列表,每個在元素IList傳入的內容是鍵入的「Item」,傳入的IList的具體類型是NOT List,因此null檢查成功,方法返回null。當然,我的斷點恰好位於該行之後,因此它看起來像方法不是'沒有被引用,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(); 
    } 
相關問題