[英]How to get selected TreeViewItem items header?
我正在从Dictonary<int, string> myFirstDictionary
(第一级)和Dictionary<string, int> mySecondDictionary
(第二级) Dictionary<string, int> mySecondDictionary
:
XAML:
<TreeView Name="myTreeView"/>
代码隐藏:
TreeViewItem item;
foreach (var dict1 in myFirstDictionary)
{
item = TreeViewItem();
item.Header = dict1.Value;
foreach (var dict2 in mySecondDictionary)
if (dict1.Key == dict2.Value)
item.Items.Add(dict2.Key);
myTreeView.Items.Add(item);
}
这工作得很好。 我可以使用以下代码轻松获取第一级 TreeViewItem 的 Header:
TreeVeiwItem tvi = myTreeView.SelectedItem as TreeViewItem;
MessageBox.Show(tvi.Header.ToString());
问题是我不知道如何获得二级 TreeViewItem 的 Header (从dict2.Key
填充的dict2.Key
。
另一种方法是从任何选定项目中提取项目标题。
string itemHeader = ((HeaderedItemsControl)myTreeView.SelectedItem ).Header.ToString()
试试这个方法:
TreeViewItem tvItem = null;
tvItem = ContainGenerator.ContainerFromItem(myTreeView.SelectedItem) as TreeViewItem;
MessageBox.Show(tvItem.Header);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.