Is there a way to get the selected item's parent's index in a TreeView
in WPF? I can loop through the items and then come with an index of the selected item but getting TreeViewItem
's Paren
's index seems complicated.
Any ideas?
Found a great(for my needs) solution. I will share it for future references.
private int? GetTreeViewItemParentIndex(TreeViewItem Item)
{
Int32 index = 0;
foreach (var _item in treeView1.Items)
{
if (_item == Item.Parent)
{
return index;
}
index++;
}
return null;
//throw new Exception("No parent window detected");
}
private void treeView1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TreeViewItem SelectedNode = ((TreeViewItem)((TreeView)sender).SelectedItem);
int? ParentIndex = GetTreeViewItemParentIndex(SelectedNode);
if (ParentIndex != null)
{
MessageBox.Show(ParentIndex.ToString());
}
else
{
MessageBox.Show("No parent detected");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.