سؤال

I am pulling data through from my SQL Database which is displaying all the tables in that database in the 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.

Here is an example.

These are my Database tables:

enter image description here

So when i run my code it all works fine and pulls through the tables to my TreeView:

enter image description here

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.

I have written my SP to get the all the columns:

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.)

هل كانت مفيدة؟

المحلول

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:

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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top