public bool Sweep(bool editMode, DataTemplate singleViewTemplate, DataTemplate tabbedViewTemplate, IServiceProvider serviceProvider) { if (this.sources.Count == 0) { // Nothing added back to this slot, so we're toast. Remove our old content (close all views) and die. SetSlotPanelContent(null); CloseRemainingViews(new Dictionary<ViewSource, ViewSite>()); return false; } var newSites = new Dictionary<ViewSource, ViewSite>(); if (this.sources.Count == 1) { var presenter = this.slotPanelChild as ContentPresenter; // We need our content to be a simple ContentPresenter. If it isn't, we need to replace. if (presenter == null) { presenter = new ContentPresenter() { ContentTemplate = singleViewTemplate }; } // Need to call this regardless, as it ensures that it has been added to the slot panel SetSlotPanelContent(presenter); ViewSite site; if (!this.sites.TryGetValue(this.sources[0], out site)) { // NOTE: Passing singleViewTemplate here has no effect -- the important thing is that it's set as the // content template of the presenter (above). site = new ViewSite(this, this.sources[0], singleViewTemplate, editMode, serviceProvider); } else { this.sites.Remove(this.sources[0]); } presenter.Content = site; if (presenter.IsLoaded) { OnViewPresenterLoaded(presenter, null); } else { presenter.Loaded += OnViewPresenterLoaded; } newSites.Add(this.sources[0], site); } else { var tabControl = this.slotPanelChild as ViewTabControl; // We need our content to be a tab control. If it isn't, we need to replace. if (tabControl == null) { tabControl = new ViewTabControl(); } // Call regardless, in case the layout control didn't have its template applied last time... SetSlotPanelContent(tabControl); for (int i = 0; i < this.sources.Count; i++) { ViewSource source = this.sources[i]; ViewSite site; if (!this.sites.TryGetValue(source, out site)) { site = new ViewSite(this, source, tabbedViewTemplate, editMode, serviceProvider); } else { this.sites.Remove(source); } int index = tabControl.Items.IndexOf(site); if (index < i) { Debug.Assert(index == -1, "A view site has been added twice?!?"); tabControl.Items.Insert(i, site); } else { while (index > i) { // Other (presumably old) view contents are in the way. Remove them. tabControl.Items.RemoveAt(i); index--; } } newSites.Add(source, site); } while (tabControl.Items.Count > this.sources.Count) { tabControl.Items.RemoveAt(tabControl.Items.Count - 1); } if (tabControl.SelectedIndex < 0) { tabControl.SelectedIndex = 0; } } CloseRemainingViews(newSites); this.sites = newSites; return true; }
public bool Sweep(bool editMode, DataTemplate singleViewTemplate, DataTemplate tabbedViewTemplate, IServiceProvider serviceProvider) { if (this.sources.Count == 0) { // Nothing added back to this slot, so we're toast. Remove our old content (close all views) and die. SetSlotPanelContent(null); CloseRemainingViews(new Dictionary <ViewSource, ViewSite>()); return(false); } var newSites = new Dictionary <ViewSource, ViewSite>(); if (this.sources.Count == 1) { var presenter = this.slotPanelChild as ContentPresenter; // We need our content to be a simple ContentPresenter. If it isn't, we need to replace. if (presenter == null) { presenter = new ContentPresenter() { ContentTemplate = singleViewTemplate }; } // Need to call this regardless, as it ensures that it has been added to the slot panel SetSlotPanelContent(presenter); ViewSite site; if (!this.sites.TryGetValue(this.sources[0], out site)) { // NOTE: Passing singleViewTemplate here has no effect -- the important thing is that it's set as the // content template of the presenter (above). site = new ViewSite(this, this.sources[0], singleViewTemplate, editMode, serviceProvider); } else { this.sites.Remove(this.sources[0]); } presenter.Content = site; if (presenter.IsLoaded) { OnViewPresenterLoaded(presenter, null); } else { presenter.Loaded += OnViewPresenterLoaded; } newSites.Add(this.sources[0], site); } else { var tabControl = this.slotPanelChild as ViewTabControl; // We need our content to be a tab control. If it isn't, we need to replace. if (tabControl == null) { tabControl = new ViewTabControl(); } // Call regardless, in case the layout control didn't have its template applied last time... SetSlotPanelContent(tabControl); for (int i = 0; i < this.sources.Count; i++) { ViewSource source = this.sources[i]; ViewSite site; if (!this.sites.TryGetValue(source, out site)) { site = new ViewSite(this, source, tabbedViewTemplate, editMode, serviceProvider); } else { this.sites.Remove(source); } int index = tabControl.Items.IndexOf(site); if (index < i) { Debug.Assert(index == -1, "A view site has been added twice?!?"); tabControl.Items.Insert(i, site); } else { while (index > i) { // Other (presumably old) view contents are in the way. Remove them. tabControl.Items.RemoveAt(i); index--; } } newSites.Add(source, site); } while (tabControl.Items.Count > this.sources.Count) { tabControl.Items.RemoveAt(tabControl.Items.Count - 1); } if (tabControl.SelectedIndex < 0) { tabControl.SelectedIndex = 0; } } CloseRemainingViews(newSites); this.sites = newSites; return(true); }