gpt4 book ai didi

c# - 我如何从选定的 ListView 项目中获取名称?

转载 作者:数据小太阳 更新时间:2023-10-29 02:11:31 24 4
gpt4 key购买 nike

我有一个 ListView,它具有到 TreeView 的数据绑定(bind),而 TreeView 是数据绑定(bind)到 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?

关于c# - 我如何从选定的 ListView 项目中获取名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5339999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com