public void CreateTabFor(string header, ImageSource icon, bool allowDelete, IViewPlugin viewPlugin, UIElement viewElement, object dataInstance, WellKnownView view, bool select)
        {
            // If allowdelete = false, we also hide the header
            var newTab = new TabItem {
                Header = header, Icon = icon, AllowDelete = allowDelete, ShowHeader = allowDelete, Tag = viewPlugin, WellKnownView = view
            };

            if (viewElement != null)
            {
                newTab.Content = viewElement;
            }

            if (newTab.Content is IControllableTab)
            {
                IControllableTab tab = (IControllableTab)newTab.Content;

                // Create a textblock and bind it to the header property of the tab slave
                TextBlock tb = new TextBlock();
                tb.SetBinding(TextBlock.TextProperty, new Binding("Title")
                {
                    Source = tab
                });

                newTab.Header = tb;

                // Attach the RequestCloseTab event
                tab.RequestCloseTab += delegate { TabHost.RemoveItem(newTab); };

                TabHost.HeaderContent = tab.CustomHeaderContent;
            }

            if (newTab.Content is IPersistableTab)
            {
                Debug.Assert(dataInstance != null, "Data instance can not be null");

                IPersistableTab tab = (IPersistableTab)newTab.Content;

                tab.LoadData(dataInstance);
            }

            TabHost.Items.Add(newTab);

            if (select)
            {
                TabHost.SelectedItem = newTab;
            }
        }
        public void PersistChildren()
        {
            var persistState = new PersistState();

            foreach (TabItem tab in TabHost.Items)
            {
                // Save child grid column-sizes
                var grid = tab.GetChildrenOf <Grid>().FirstOrDefault();

                if (grid != null && GridColumnSizeHelper.GetSaveGridColumnsSize(grid))
                {
                    GridColumnSizeHelper.SaveColumnSizes(grid);
                }

                // Save open tab's state
                if (tab.Content is IPersistableTab)
                {
                    IPersistableTab saveTab = (IPersistableTab)tab.Content;

                    var data = saveTab.SaveData();

                    if (data != null)
                    {
                        persistState.Tabs.Add(new TabState
                        {
                            PluginType   = saveTab.Plugin.GetType(),
                            ViewType     = saveTab.ViewType,
                            DataInstance = saveTab.SaveData()
                        });
                    }
                }
            }

            persistState.SelectedTabIndex = TabHost.SelectedIndex;

            ClientState.Current.Context.SaveSettingTo("state.xml", persistState);
        }