2017-06-13 102 views
0

我的目標很簡單。 DataTable中包含說5個表的數據。在我的TreeView中,我必須將表名稱作爲節點和列名稱作爲特定節點(表名)的子節點。我在VS2015中使用C#語言。我正在使用WAF架構。如何將數據庫中的數據綁定到TreeView

這裏是我試過至今:

查看: -

<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的對象。

在使用上面的代碼時,我只能綁定treeview中的父節點,但不能綁定子元素。

回答

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 

    } 
相關問題