private void serverExplorer_Connected(object sender, EventArgs e)
 {
     if (treeView.Items.Count > 0)
     {
         //TreeViewItem tvi = treeView.ItemContainerGenerator.ContainerFromItem(treeView.Items[0]) as TreeViewItem;
         //RefreshImage(tvi);
     }
     databaseDetails.Clear();
     databasesDetail = null;
     procedureDetail = null;
     proceduresDetails.Clear();
     serverDetail = null;
     tableDetail  = null;
     tablesDetails.Clear();
     userDetail  = null;
     usersDetail = null;
     viewDetail  = null;
     viewsDetails.Clear();
 }
        private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs <object> e)
        {
            // TODO: I can assign the panel and DataContext generically
            IElement node = e.NewValue as IElement;

            if (node != null)
            {
                if (node.Name == "Connections")
                {
                    if (connectionDetail == null)
                    {
                        connectionDetail             = new ConnectionDetail();
                        connectionDetail.DataContext = serverExplorer;
                    }
                    DetailPanel.Content = connectionDetail;
                }
                else if (node.TypeName == "Server")
                {
                    if (serverDetail == null)
                    {
                        serverDetail             = new ServerDetail();
                        serverDetail.DataContext = node;
                    }
                    DetailPanel.Content = serverDetail;
                }
                else if (node.TypeName == nameof(Query))
                {
                    if (queryEditor == null)
                    {
                        queryEditor             = new QueryEditor();
                        queryEditor.DataContext = node;
                    }
                    DetailPanel.Content = queryEditor;
                }
                else if (node.Name == "Databases")
                {
                    if (databasesDetail == null)
                    {
                        databasesDetail             = new DatabasesDetail();
                        databasesDetail.DataContext = node;
                    }
                    DetailPanel.Content = databasesDetail;
                }
                else if (node.Name == "Users")
                {
                    if (usersDetail == null)
                    {
                        usersDetail             = new UsersDetail();
                        usersDetail.DataContext = node;
                    }
                    DetailPanel.Content = usersDetail;
                }
                else if (node.TypeName == "Database")
                {
                    if (!databaseDetails.ContainsKey(node.Name))
                    {
                        DatabaseDetail dd = new DatabaseDetail();
                        dd.DataContext = node;
                        databaseDetails.Add(node.Name, dd);
                    }
                    DetailPanel.Content = databaseDetails[node.Name];
                }
                else if (node.Name == "Tables")
                {
                    if (!tablesDetails.ContainsKey(node.Parent.Name))
                    {
                        TablesDetail td = new TablesDetail();
                        td.DataContext = node;
                        tablesDetails.Add(node.Parent.Name, td);
                    }
                    DetailPanel.Content = tablesDetails[node.Parent.Name];
                }
                else if (node.Name == "Procedures")
                {
                    if (!proceduresDetails.ContainsKey(node.Parent.Name))
                    {
                        ProceduresDetail pd = new ProceduresDetail();
                        pd.DataContext = node;
                        proceduresDetails.Add(node.Parent.Name, pd);
                    }
                    DetailPanel.Content = proceduresDetails[node.Parent.Name];
                }
                else if (node.Name == "Views")
                {
                    if (!viewsDetails.ContainsKey(node.Parent.Name))
                    {
                        ViewsDetail vd = new ViewsDetail();
                        vd.DataContext = node;
                        viewsDetails.Add(node.Parent.Name, vd);
                    }
                    DetailPanel.Content = viewsDetails[node.Parent.Name];
                }
                else if (node.TypeName == nameof(Table))
                {
                    if (tableDetail == null)
                    {
                        tableDetail = new TableDetail();
                    }
                    Table     t    = node as Table;
                    IDatabase d    = (IDatabase)t.Parent;
                    Task      task = new Task(() => { Dispatcher.Invoke(() => tableDetail.DataContext = d.GetTable(node.Name)); });
                    task.Start();
                    DetailPanel.Content = tableDetail;
                }
                else if (node.TypeName == nameof(Procedure))
                {
                    if (procedureDetail == null)
                    {
                        procedureDetail = new ProcedureDetail();
                    }
                    Procedure p    = node as Procedure;
                    IDatabase d    = (IDatabase)p.Parent;
                    Task      task = new Task(() => { Dispatcher.Invoke(() => procedureDetail.DataContext = d.GetProcedure(node.Name)); });
                    task.Start();
                    DetailPanel.Content = procedureDetail;
                }
                else if (node.TypeName == nameof(View))
                {
                    if (viewDetail == null)
                    {
                        viewDetail = new ViewDetail();
                    }
                    View      v    = node as View;
                    IDatabase d    = (IDatabase)v.Parent;
                    Task      task = new Task(() => { Dispatcher.Invoke(() => viewDetail.DataContext = d.GetView(node.Name)); });
                    task.Start();
                    DetailPanel.Content = viewDetail;
                }
                else if (node.TypeName == nameof(User))
                {
                    if (userDetail == null)
                    {
                        userDetail = new UserDetail();
                    }
                    DetailPanel.Content = userDetail;
                }
            }
        }