private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            SQLTreeItem selected = ((SQLTreeItem)MainTree.SelectedItem);

            ViewModel.DeleteSelectedNode(MainTree.Items, selected);
            ViewModel.SaveTreeView(MainTree);
        }
        public void AddNewSQLNode(TreeView treeView, string headerText, string sQLText, string keyFields, string schemaSQL)
        {
            if (((headerText ?? "") != "") && ((sQLText ?? "") != "") && ((schemaSQL ?? "") != ""))
            {
                SQLTreeItem newItem = new SQLTreeItem();
                newItem.Header    = headerText;
                newItem.SQL       = sQLText;
                newItem.KeyFields = (keyFields != null) ? keyFields.Split(';') : new string[0];
                newItem.SchemaSQL = schemaSQL;


                if (treeView.SelectedItem != null)
                {
                    if (((SQLTreeItem)treeView.SelectedItem).ItemType == SQLTreeItem.SQLType.SQL)
                    {
                        newItem.ItemType = SQLTreeItem.SQLType.Schema;
                    }
                    else if (((SQLTreeItem)treeView.SelectedItem).ItemType == SQLTreeItem.SQLType.Schema)
                    {
                        newItem.ItemType = SQLTreeItem.SQLType.SQL;
                    }
                    ((SQLTreeItem)treeView.SelectedItem).Items.Add(newItem);
                }
                else
                {
                    treeView.Items.Add(newItem);
                }
            }
        }
        public void DeleteSelectedNode(ItemCollection collection, SQLTreeItem itemToDelete)
        {
            foreach (SQLTreeItem i in collection)
            {
                if (i.Items.Count > 0)
                {
                    if (i == itemToDelete)
                    {
                        collection.Remove(i);
                        break;
                    }

                    int index = i.Items.IndexOf(itemToDelete);
                    if (index > -1)
                    {
                        i.Items.RemoveAt(index);
                        break;
                    }
                    else
                    {
                        DeleteSelectedNode(i.Items, itemToDelete);
                    }
                }
            }
        }
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            AddNewSQLNodeWindow addNewSQLNodeWindow = new AddNewSQLNodeWindow();
            SQLTreeItem         selected            = ((SQLTreeItem)MainTree.SelectedItem);

            addNewSQLNodeWindow.ViewModel.HeaderName = selected.Header.ToString();
            addNewSQLNodeWindow.ViewModel.SQLText    = selected.SQL;
            addNewSQLNodeWindow.ViewModel.SchemaSQL  = selected.SchemaSQL;

            if (selected.KeyFields != null)
            {
                string fields = "";
                foreach (string str in selected.KeyFields)
                {
                    if (str.Trim() != "")
                    {
                        fields = fields + str + "; ";
                    }
                }
                addNewSQLNodeWindow.ViewModel.KeyFields = fields;
            }

            addNewSQLNodeWindow.ShowDialog();

            selected.Header    = addNewSQLNodeWindow.ViewModel.HeaderName;
            selected.SQL       = addNewSQLNodeWindow.ViewModel.SQLText;
            selected.KeyFields = addNewSQLNodeWindow.ViewModel.KeyFields.Split(';');
            selected.SchemaSQL = addNewSQLNodeWindow.ViewModel.SchemaSQL;
            ViewModel.SaveTreeView(MainTree);
        }
        public void RefreshSelectedSQLItemSchema(TreeView treeView)
        {
            SQLTreeItem item = ((SQLTreeItem)treeView.SelectedItem);

            item.Items.Clear();

            List <string> columnSchemas = _SQLHandler.GetSQlSchemaFromSQLItem(item, ConnectionStr);

            foreach (string colname in columnSchemas)
            {
                item.Items.Add(new SQLTreeItem()
                {
                    Header   = colname,
                    ItemType = SQLTreeItem.SQLType.Schema
                });
            }
        }