[英]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.