我正在从Dictonary<int, string> myFirstDictionary
(对于第一级)和Dictionary<string, int> mySecondDictionary
(对于第二级)中填充两级TreeView:
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的第一级标题:
TreeVeiwItem tvi = myTreeView.SelectedItem as TreeViewItem;
MessageBox.Show(tvi.Header.ToString());
问题是我不知道如何获取第二级TreeViewItem的标头(从dict2.Key
填充的标头。
尝试这种方式:
从任何选定项目中提取项目标题的另一种方法。