0
嘗試獲取樹視圖中選定的父節點標題和子節點標題,但出現無法將字符串轉換爲TreeViewItem的錯誤。TreeView獲取選定節點問題
private void tvSQLTasks_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
//error is here
TreeViewItem child = (TreeViewItem)tvSQLTasks.SelectedItem;
if (child.Parent is TreeViewItem)
{
TreeViewItem parent = (TreeViewItem)child.Parent;
SQLTask _task = SQLTasks.Where(t => t._rootNode.Equals(parent.Header.ToString()) && t._childNode.Equals(child.Header.ToString())).FirstOrDefault();
if (_task != null)
{
txtTaskDescription.Text = _task._taskDescription;
}
}
}
下面的是我如何添加節點
private void AddRootNode(string _nodeName)
{
tvSQLTasks.Items.Add(new TreeViewItem() { Header = _nodeName });
}
private void AddChildNode(string _rootNode, string _childeNode)
{
foreach (TreeViewItem node in tvSQLTasks.Items)
{
if (node.Header.Equals(_rootNode))
{
(node as TreeViewItem).Items.Add(_childeNode);
}
}
}