开发者

Select Rootnode from a WPF-TreeView

开发者 https://www.devze.com 2023-02-18 10:15 出处:网络
How to programmatically select the rootnode from a wpf treeview in c# that is databinde开发者_开发技巧t with a xmlDataProvider?

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.

0

精彩评论

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