Select Rootnode from a WPF-TreeView
How to programmatically select the rootnode from a wpf treeview in c# that is databinde开发者_开发技巧t with a xmlDataProvider?
In my case the Rootnode is from DataType="cards":
<cards>
<category />
<card />
<card />
<card />
</cards>
I tried this:
//AddNode
xmlDataProvider.Document.DocumentElement["cards"].AppendChild(newNode);
//Select rootnode
treeView.SelectedValuePath = "cards";
//the hard way:
//treeView.SelectedValuePath = ((XmlNode)treeView.Items.GetItemAt(0)).Name.LocalName";
treeView.Focus();
but the Rootnode isn´t selected :-(
I made it :-)
var treeViewItem = treeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem;
treeViewItem.Focus();
(treeView.Items[0] as TreeViewItem).Focus(); should work, i think.
精彩评论