多级TreeView

时间:2016-01-07 12:23:03

标签: c# wpf treeview hierarchicaldatatemplate

我的目标是像这样创建TreeView:

 alias1
 --TABLES  ( constant string ) 
 ----TABLE_NAME11
 ----TABLE_NAME12 
 ----TABLE_NAME13
 ---- ...
 --INDEXES  ( constant string ) 
 ----INDEX_NAME11
 ----INDEX_NAME12
 ---- ...
alias2
--TABLES ( constant string ) 
----TABLE_NAME21
----TABLE_NAME22 
----TABLE_NAME23
---- ...
--INDEXES  ( constant string ) 
----INDEX_NAME21
----INDEX_NAME22
.... (n)
alias(n)
++TABLES  ( constant string ) 
++INDEXES ( constant string ) 

我想我已经阅读了有关TreeView,HierarhicalDataTemplate的stackoverflow的每个主题,但我不能使结构与此类似。

我尝试将每个主题调整到我的视图模型,但我真的不能。

所以我想我应该一步一步地做。

  1. 将别名绑定为TreeViewItem(DONE)。
  2. 将别名扩展为常量字符串'表格'和' INDEXES'。
  3. 我的结构总是这样:

     alias1   
     alias2 
     alias3 
     alias4
    

    它没有任何扩展别名的按钮。 它只是别名列表。

    我的整个XML:             

        <Grid>
            <TabControl>
                <TabItem Header="Map">Mapa</TabItem>
                <TabItem Header="History">History</TabItem>
                <TabItem Header="Aliases">
                <Grid>
                    <TreeView Name="AliasesTree" ItemsSource="{Binding aliases}">
                        <TreeView.ItemContainerStyle>
                            <Style TargetType="{x:Type TreeViewItem}">
                                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                                <Setter Property="FontWeight" Value="Normal" />
                                <Style.Triggers>
                                    <Trigger Property="IsSelected" Value="True">
                                        <Setter Property="FontWeight" Value="Bold" />
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </TreeView.ItemContainerStyle>
    
                        <TreeView.Resources>
                            <HierarchicalDataTemplate DataType="{x:Type models:ConnectionModel }" 
                                                      ItemsSource="{Binding aliases}"
                            >
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding alias}" />
                                </StackPanel>
                            </HierarchicalDataTemplate>
    
                            <HierarchicalDataTemplate DataType="string">
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="TABLES" />
                                    <TextBlock Text="INDEXES" />
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.Resources>
                    </TreeView>
                </Grid>
                </TabItem>
            </TabControl>
        </Grid>
    

    我的page.cs文件:

          public partial class SideBar : Page
    {
        public List<ConnectionModel> aliases = ConnectionUtilities.LoadConnections();
    
        public SideBar()
        {
            InitializeComponent();
    
            LoadSideBar();
    
            DataContext = new { aliases = this.aliases , this.DataContext  };
    
            // AliasesTree.ItemsSource = aliases;
    
        }
    
        public async Task LoadSideBar()
        {
            await LoadAliasTreeView();
        }
    
    
        public async Task LoadAliasTreeView()
        {
    
            //aliasTreeView.ItemsSource = ConnectionUtilities.LoadConnections();
            OdbcDataAdapter MainOdbcAdapter = new OdbcDataAdapter();
            String commandString = "";
    
            foreach (ConnectionModel connection in aliases)
            {
                await Dispatcher.InvokeAsync(
                    async () =>
                    {
                        using (OdbcConnection con = new OdbcConnection("DSN=" + connection.dsn.ToString()
                                                                    + ";UID=" + connection.username.ToString()
                                                                    + ";PWD=" + connection.password.ToString()))
                        {
                            using (MainOdbcAdapter = new OdbcDataAdapter(commandString, con))
                            {
                                if (con.State == ConnectionState.Closed)
                                    con.Open();
                                await Task.Run(() =>
                                {
                                    using (DataTable tableschema = con.GetSchema("TABLES"))
                                    {
                                        // first column name
                                        foreach (DataRow row in tableschema.Rows)
                                        {
                                            connection.tables.Add(new TableModel() { TABLE_NAME = row["TABLE_NAME"].ToString() });
                                            //  MessageBox.Show(  row["TABLE_NAME"].ToString()); Sprawdzone, dziala, pokazuje nazwy tabel
                                        }
                                    }
                                });
                            }
                        }
                    });
    
            }
    
            //aliasTreeView.ItemsSource = aliases;
    
        }
    

    我的模特

     namespace ProgDB4.Model
     {
    [Serializable()]
    public class ConnectionModel
    {
    
        private int    _id;
    
        private string _dsn;
        private string _alias   ;
    
        private string _host    ;
        private string _port    ;
    
        private string _database;
    
        private string _username;
        private string _password;
    
        public List<TableModel> tables  = new List<TableModel>(); 
        public List<IndexModel> indexes = new List<IndexModel>();
    
        [System.Xml.Serialization.XmlElement("id")]
        public int id
        {
            get { return _id; }
            set { if (value != _id) _id = value; }
        }
    
        [System.Xml.Serialization.XmlElement("dsn")]
        public string dsn
        {
            get { return _dsn; }
            set { if (value != _dsn) _dsn = value; }
        }
    
        [System.Xml.Serialization.XmlElement("alias")]
        public string alias
        {
            get { return _alias; }
            set { if (value != _alias) _alias = value; }
        }
    
        [System.Xml.Serialization.XmlElement("host")]
        public string host
        {
            get { return _host; } 
            set { if (value != _host) _host = value; }
        }
    
        [System.Xml.Serialization.XmlElement("port")]
        public string port
        {
            get { return _port; }
            set { if (value != _port) _port = value; }
        }
    
        [System.Xml.Serialization.XmlElement("username")]
        public string username
        {
            get { return _username; }
            set { if (value != _username) _username = value; }
        }
    
        [System.Xml.Serialization.XmlElement("password")]
        public string password
        {
            get { return _password; }
            set { if (value != _password) _password = value; }
        }
    
        [System.Xml.Serialization.XmlElement("database")]
        public string database
        {
            get { return _database; }
            set { if (value != _database) _database = value; }
        }
    
        public ConnectionModel()
        {
            _id = 1;
            _dsn    = "";
            _alias  = "New Conn";
    
            _host = "http://192.168.0.1";
            _port = "22";
    
            _database = "database_name";
    
            _username = "login";
            _password = "pass";
        }
    
        public ConnectionModel( int newItemId )
        {
            _id = newItemId;
            _dsn    = "";
            _alias  = "New Conn";
    
            _host = "http://192.168.0.1";
            _port = "22";
    
            _database = "database_name";
    
            _username = "login";
            _password = "pass" ;
        }
    
    
    }
    
    
    public class TableModel
    {
        public string TABLE_CAT     { get; set; }  = "";
        public string TABLE_SCHEM   { get; set; }  = "";
        public string TABLE_NAME    { get; set; }  = "";
        public string TABLE_TYPE    { get; set; }  = "";
        public string REMARKS       { get; set; } = "";
    }
    
    public class IndexModel
    {                                  
        public string TABLE_CAT        { get; set; }  = "";
        public string TABLE_SCHEM      { get; set; }  = "";
        public string TABLE_NAME       { get; set; }  = "";
        public int    NON_UNIQUE       { get; set; }  = 0 ;
        public string INDEX_QUALIFIER  { get; set; }  = "";
        public string INDEX_NAME       { get; set; }  = "";
        public int    TYPE             { get; set; }  = 0 ;
        public int    ORDINAL_POSITION { get; set; }  = 0 ;
        public string COLUMN_NAME      { get; set; }  = "";
        public string ASC_OR_DESC      { get; set; }  = "";
        public int    CARDINALITY      { get; set; }  = 0 ;
        public int    PAGES            { get; set; }  = 0 ;
        public string FILTER_CONDITION { get; set; }  = "";
    
    }
    
    
    }
    

    感谢您的任何建议。

    我的下一步将是: 3.展开&#34; TABLES&#34;到TableModel.TABLE_NAME中的表名列表。

0 个答案:

没有答案