如何将数据从数据库绑定到TreeView

时间:2017-06-13 10:08:58

标签: c# wpf visual-studio-2015 treeview waf-framework-c#

我的目标很简单。我在DataTable中有数据包含5个表。在我的TreeView中,我必须将表名作为节点和列名绑定为特定节点(表名)的子节点。我在VS2015中使用C#语言。我正在使用WAF Architecture。

这是我到现在为止所做的:

查看: -

<TreeView x:Name="treeQueryBuilder"  ItemsSource="{Binding dictTree.Keys}">                         

            <TreeView.ItemTemplate >
            <HierarchicalDataTemplate >
                <TextBlock FontWeight="Bold" Text="{Binding }"/>
                <HierarchicalDataTemplate.ItemTemplate  >
                    <DataTemplate >
                        <TextBlock Text="{Binding}" Width="100"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
</TreeView>

视图模型: -

public Dictionary<string, List<string>> dictTree
        {
            get
            {
                if (_TreeNode == null)
                    _TreeNode = new Dictionary<string, List<string>>();
                    return _TreeNode;
            }
            set { SetProperty(ref _TreeNode, value); }
        }

  public List<string> ChildNodes
        {
            get
            {
                if (_ChildNodes == null)
                    _ChildNodes = new List<string>();
                return _ChildNodes;
            }
            set { SetProperty(ref _ChildNodes, value); }
        }

控制器: -

private void PopulateDBTables()
        {
            try
            {
                DBConnect.ConnectToOracleDB(false);
                Table_Query = "SELECT TABLE_NAME FROM DBA_TABLES WHERE OWNER='DataBase' ORDER BY TABLE_NAME";
                Obj_QueryBuilderViewModel.dtgrdTableData = DBConnect.RetrieveFromTable(Table_Query);
                foreach (DataRow TableName in Obj_QueryBuilderViewModel.dtgrdTableData.Rows)
                {
                    Obj_QueryBuilderViewModel.ParentNode = new TreeNode(TableName[0].ToString());
                    Obj_QueryBuilderViewModel.dictTree.Add(Obj_QueryBuilderViewModel.ParentNode.Text, ADDCHILDNODES(TableName[0].ToString(),Obj_QueryBuilderViewModel.ParentNode));

                }
            }
            catch (Exception EX)
            {
                App.DisplayMessage(EX.Message);
            }
        }


        private List<string> ADDCHILDNODES(string Table_Name, TreeNode parentNode)
        {
            try
            {
                DBConnect.ConnectToOracleDB(false);
                Column_Query = "select column_name from dba_tab_cols where owner='EDS' and table_name='" + Table_Name + "'order by column_name";
                dt_Columns = DBConnect.RetrieveFromTable(Column_Query);

                if (Obj_QueryBuilderViewModel.ChildNodes.Count > 0)
                {
                    Obj_QueryBuilderViewModel.ChildNodes = null;
                }
                foreach (DataRow Column_Name in dt_Columns.Rows)
                {
                    Obj_QueryBuilderViewModel.ChildNode = new TreeNode(Column_Name[0].ToString());
                    Obj_QueryBuilderViewModel.ChildNodes.Add(Obj_QueryBuilderViewModel.ChildNode.Text);
                }

            }
            catch (Exception EX)
            {

            }

            return Obj_QueryBuilderViewModel.ChildNodes;

        }

此处 Obj_QueryBuilderViewModel 是ViewModel的对象。

使用上面的代码时,我只能绑定树视图中的父节点,而不能绑定子元素。

1 个答案:

答案 0 :(得分:0)

最后,这是我发现的。

<TreeView x:Name="treeQueryBuilder"  ItemsSource="{Binding DictClass}">                         

            <TreeView.ItemTemplate >
            <HierarchicalDataTemplate Itemsource="{Binding Columnnames}" >
                <TextBlock FontWeight="Bold" Text="{Binding Tablenames }"/>
                <HierarchicalDataTemplate.ItemTemplate  >
                    <DataTemplate >
                        <TextBlock Text="{Binding}" Width="100"/>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
</TreeView>

我已经拿了一个类,并在那里写了类的属性

 public Class DictClass
    {
    public string Tablenames{get;set;}//as Parentnodes

    public list<string> Columnnames{get;set;}//as Childnodes

    }