[英]Selected Item of a TreeView
I am pulling data through from my SQL Database
which is displaying all the tables in that database in the TreeView
. 我正在从
SQL Database
提取数据,该SQL Database
数据库在TreeView
显示该数据库中的所有表。 I am now stuck on the next bit which is when I click on one of the tables names in the tree view all the column names in that table show as TreeItems
. 我现在停留在下一位,即单击树视图中的表名称之一时,该表中的所有列名称均显示为
TreeItems
。
Here is an example. 这是一个例子。
These are my Database tables: 这些是我的数据库表:
So when i run my code it all works fine and pulls through the tables to my TreeView: 因此,当我运行我的代码时,一切正常,并将表拖到我的TreeView中:
I did this using this code; 我是使用这段代码完成的;
TreeViewItem treeItem = null;
treeItem = new TreeViewItem();
treeItem.Header = "Standards";
treeItem.Tag = "Branch";
foreach (Standards Standard in _Standards)
{
TreeViewItem createdTV;
createdTV = new TreeViewItem() { Header = Standard.Table_name };
treeItem.Items.Add(createdTV);
}
ToDoList.Items.Add(treeItem);
The problem comes when I try and click on one of the tables from the TreeView, I want to get all of the columns in that table and display as TreeView items under the selected Table. 当我尝试从TreeView中单击其中一个表时,问题就来了,我想获取该表中的所有列并在选定表下显示为TreeView项。
I have written my SP to get the all the columns: 我已经编写了SP以获取所有列:
public List<StandardDefinition> GetStandardFields(string _selectedStandard)
{
_SP = new StoredProcedures(_connection);
return _SP.GetColumnHeaders(_selectedStandard).ToList();
}
I just need to find the SelectedItem in the treeview (I only want it to find the SelectedItem when you click a Table name under standards, not when you click any TreeView item.)
我只需要在树视图中找到SelectedItem
(I only want it to find the SelectedItem when you click a Table name under standards, not when you click any TreeView item.)
You need to add a SelectedItemChanged event to the TreeView control, and in there you can get the selected item and check if it's a child of the Standards item, as follows: 您需要向TreeView控件中添加SelectedItemChanged事件,然后在其中可以获取所选项目并检查它是否是Standards项目的子项,如下所示:
private void ToDoList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeViewItem selItem = ((TreeViewItem)ToDoList.SelectedItem);
//If the parent is a treeviewitem, this is a child node.
if (selItem.Parent is TreeViewItem)
{
//Check if the parent treeviewitem is the standards node.
if (((TreeViewItem)selItem.Parent).Header.ToString() == "Standards")
{
//Do stuff here.
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.