I need开发者_如何学C some help implementing a multi-select on a listBbox control. At the moment I am able to display the information for one selection using the code below however I undestand it is possible to use e.added in my itemsSource to enable multiselect. Would appreciate any help. Thanks - Ben
private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
//TODO: Add event handler implementation here.
ContactList selectedContact = contactsList.SelectedItem as ContactList;
tagsList.ItemsSource = new List<ContactList> { selectedContact };//??
}
Your question is hard to comprehend and vague, i'll just assume you want to set that tagsList.ItemsSource to the all the selected ContactLists.
private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
List<ContactList> list = new List<ContactList>();
foreach (object contactList in contactsList.SelectedItems)
{
list.Add(contactList as ContactList);
}
tagsList.ItemsSource = list;
}
Edit: If you in fact haven't set ListBox.SelectionMode
you should of course do that first.
Multi-selection capabilites are built in to the listbox control...
Have you set ListBox.SelectionMode? http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.selectionmode.aspx
try this. it will add items to tagsList
when selection made on contactsList
using e.AddedItems
private void contactsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
tagsList.Items.Add(((ListBoxItem)e.AddedItems[0]).Content.ToString());
}
Multi-Selection in a list box is achieved by setting the SelectionMode property on the list box instance to either Extended or Multiple (Extended means you need to hold down the SHIFT key and with Multiple you dont).
lstBox.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
To access the selected items you then get the value of the lstBox.SelectedItems property.
In the example you give - you could do something similar to the following:
tagsList.ItemsSource = contactList.SelectedItems;
Hope this helps :)
精彩评论