I'm trying to Cast from an object to a TreeviewItem and getting the next error:
"unable to cast object of type 'system.string' to type 'system.windows.controls.treeviewitem'."
For 2 days I'm searching for a solution to this, I saw many examples of explicit cast to treeviewitem so I think it legit but its not working.
foreach (Req ObjReq in reqFilter.NewList())
{
index = PM_TreeView.Items.Add(ObjReq.Name);
TreeViewItem ParentNode = new TreeViewItem();
//this is the Cast I try to do
ParentNode = (TreeViewItem)PM_TreeView.Items[index];
ParentNode.Tag = ObjReq.ID;
reqFilter["RQ_FATHER_ID"] = (ObjReq.ID.ToString());
reqFilter.KeepHierarchical = true;
if (reqFilter.NewList().Count > 0)
FillReqTreeView(reqFilter, ObjReq);
}
The main idea is to populate the List in the reqFilter to a Treeview.
The message says you are trying to cast a STRING to a TreeViewItem.
The third line in your code
index = PM_TreeView.Items.Add(ObjReq.Name);
adds a STRING to the Items collection yet the seventh tries to cast this same string to a TreeViewItem
ParentNode = (TreeViewItem)PM_TreeView.Items[index];
Instead of adding a string, add a new TreeViewItem with the Header value you want