I want to access the contents of an ItemCollection that contains the items from a Treeview (called OOB).
if (OOB.Items.Count > 0)
{
ItemCollection items = OOB.Items;
foreach (TreeViewItem node in items)
The foreach throws the runtime error:
Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Windows.Controls.TreeViewItem'.
I can SEE the information in the ItemCollection items that I want to access from the debugger:
How can I access the elements, specifically the Element Name?

The
ItemsorItemsSourcecollection of an ItemsControl contains data items of an arbitrary type, not TreeViewItems (unless you explicitly add them).The
Itemscollection of your TreeView contains XmlElements, which you can directly access like this:In case you really need access to the item container, you may use the ItemsControl's
ItemContainerGeneratorlike this: