我有一个ListView,其数据绑定到TreeView,该TreeView被数据绑定到XmlDataProvider.在ListView中,您可以一次选择多个项目.我的问题是我需要知道选择了哪些项目.我怎样才能从ListViewItems中获取名称?
XAML:
<ListView x:Name="listViewCards" DataContext="{Binding ElementName=treeViewCategory, Path=SelectedItem}" ItemsSource="{Binding XPath=./card/title, UpdateSourceTrigger=PropertyChanged}" KeyDown="listViewCards_KeyDown" />
C#:
private void listViewCards_KeyDown(object sender, KeyEventArgs e)
{
IList selectedListViewItems = listViewCards.SelectedItems;
if (selectedListViewItems.Count > 1)
{
//delete all selected items from xml:
foreach (XmlNode node in xmlNode.ChildNodes)
{
if (node.InnerText.Equals( ??? ))
{
//mark for deleting
}
}
}
}
最佳答案
The objekt of type
“System.Windows.Controls.SelectedItemCollection”
can´t convert into
“System.Windows.Controls.ListViewItem”
试试这个:
private void listViewCards_KeyDown(object sender, KeyEventArgs e)
{
IList selectedListViewItems = listViewCards.SelectedItems;
if (selectedListViewItems.Count > 1)
{
//delete all selected items from xml:
var collection = selectedListViewItems.Cast<XmlNode>(); //assuming your underlying data is XmlNode
foreach(XmlNode node in collection)
{
//do whatever you want to do with node
if (node.InnerText.Equals( ??? ))
{
//mark for deleting
}
}
}
}
相关:How to cast a System.Windows.Controls.SelectedItemCollection?