开发者

How can i get the names from the selected ListViewItems?

开发者 https://www.devze.com 2023-02-17 23:01 出处:网络
I have a ListView with a databinding to a TreeView that is databinded to a XmlDataProvider. In a ListView you can select multible items at once. My problem is that i need to know which items were sele

I have a ListView with a databinding to a TreeView that is databinded to a XmlDataProvider. In a ListView you can select multible items at once. My problem is that i need to know which items were selected. How can i get the names from the 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 sele开发者_运维百科cted 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"

Try this:

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


Related: How to cast a System.Windows.Controls.SelectedItemCollection?


listViewCards.SelectedItems;

is going to return the Type(ListViewItem most likely). You will need to cast that type, if it is of Type XmlNode.

private void listViewCards_KeyDown(object sender, KeyEventArgs e)
{
    IList selectedListViewItems = listViewCards.SelectedItems;
    if (selectedListViewItems.Count > 1)
    {
        foreach(var node in ((ListViewItem)selectedListViewItems).Tag as XMLNode)
        {
            // Find your node in whatever type contains your xml
            // Do Some deleting or what have you.
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消