C# WPF获取代码中列表框内的复选框

C# WPF获取代码中列表框内的复选框,c#,wpf,listbox,C#,Wpf,Listbox,我有这个列表框,我想搜索用户选择的项目(IsChecked=true) 我还以这种方式绑定数据: //staff is my entity object containing Id, FirstName, LastName, CellphoneNumber lstStaffs.ItemsSource = args.Result; // comes from webservice call and is Staff[] lstStaffs.UpdateLayout(); 但是我在lststaff

我有这个列表框,我想搜索用户选择的项目(IsChecked=true)

我还以这种方式绑定数据:

//staff is my entity object containing Id, FirstName, LastName, CellphoneNumber
lstStaffs.ItemsSource = args.Result; // comes from webservice call and is Staff[]
lstStaffs.UpdateLayout();
但是我在lststaff.Items中得到了Staff对象!!,那么,我如何迭代选定的(IsChecked=true)项(staff)

Tnx

来自MSDN页面:

// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.
    ContainerFromItem(myListBox.Items.CurrentItem));

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", 
    myContentPresenter);

// Do something to the DataTemplate-generated TextBlock
MessageBox.Show("The text of the TextBlock of the selected list item: "
    + myTextBlock.Text);

您必须将
SelectionMode
设置为
Multiple
Extended
,才能使其工作

如果要获取选定的项目,则需要使用SelectedItems属性您可能还需要禁用虚拟化,否则,
ContainerFromItem()
将在不可见的项目上返回
null
//staff is my entity object containing Id, FirstName, LastName, CellphoneNumber
lstStaffs.ItemsSource = args.Result; // comes from webservice call and is Staff[]
lstStaffs.UpdateLayout();
// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.
    ContainerFromItem(myListBox.Items.CurrentItem));

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", 
    myContentPresenter);

// Do something to the DataTemplate-generated TextBlock
MessageBox.Show("The text of the TextBlock of the selected list item: "
    + myTextBlock.Text);
var selectedItems = lstStaffs.SelectedItems;