private void InternalSetSelectedAnchorable(LayoutAnchorableItem anchorableToSelect) { _internalSetSelectedAnchorable = true; this.SelectedAnchorable = anchorableToSelect; _internalSetSelectedAnchorable = false; if (_anchorableListBox != null) { _anchorableListBox.Focus(); } }
void CreateAnchorableLayoutItem(LayoutAnchorable contentToAttach) { if (_layoutItems.Any(item => item.LayoutElement == contentToAttach)) return; var layoutItem = new LayoutAnchorableItem(); layoutItem.Attach(contentToAttach); ApplyStyleToLayoutItem(layoutItem); _layoutItems.Add(layoutItem); if (contentToAttach != null && contentToAttach.Content != null && contentToAttach.Content is UIElement) { InternalAddLogicalChild(contentToAttach.Content); } }
void Layout_ElementAdded(object sender, LayoutElementEventArgs e) { if (_suspendLayoutItemCreation) return; if (e.Element is LayoutDocument) { var document = e.Element as LayoutDocument; var documentItem = new LayoutDocumentItem(); documentItem.Attach(document); ApplyStyleToLayoutItem(documentItem); _layoutItems.Add(documentItem); } else if (e.Element is LayoutAnchorable) { var anchorable = e.Element as LayoutAnchorable; var anchorableItem = new LayoutAnchorableItem(); anchorableItem.Attach(anchorable); ApplyStyleToLayoutItem(anchorableItem); _layoutItems.Add(anchorableItem); } else if (e.Element is ILayoutContainer) { foreach (var document in e.Element.Descendents().OfType<LayoutDocument>().ToArray()) { var documentItem = new LayoutDocumentItem(); documentItem.Attach(document); ApplyStyleToLayoutItem(documentItem); _layoutItems.Add(documentItem); } foreach (var anchorable in e.Element.Descendents().OfType<LayoutAnchorable>().ToArray()) { var anchorableItem = new LayoutAnchorableItem(); anchorableItem.Attach(anchorable); ApplyStyleToLayoutItem(anchorableItem); _layoutItems.Add(anchorableItem); } } }
void AttachLayoutItems() { if (Layout != null) { foreach (var document in Layout.Descendents().OfType<LayoutDocument>().ToArray()) { var documentItem = new LayoutDocumentItem(); documentItem.Attach(document); ApplyStyleToLayoutItem(documentItem); _layoutItems.Add(documentItem); } foreach (var anchorable in Layout.Descendents().OfType<LayoutAnchorable>().ToArray()) { var anchorableItem = new LayoutAnchorableItem(); anchorableItem.Attach(anchorable); ApplyStyleToLayoutItem(anchorableItem); _layoutItems.Add(anchorableItem); } Layout.ElementAdded += new EventHandler<LayoutElementEventArgs>(Layout_ElementAdded); Layout.ElementRemoved += new EventHandler<LayoutElementEventArgs>(Layout_ElementRemoved); } }
void AttachAnchorablesSource(LayoutRoot layout, IEnumerable anchorablesSource) { if (anchorablesSource == null) return; if (layout == null) return; //if (layout.Descendents().OfType<LayoutAnchorable>().Any()) // throw new InvalidOperationException("Unable to set the AnchorablesSource property if LayoutAnchorable objects are already present in the model"); var anchorablesImported = layout.Descendents().OfType<LayoutAnchorable>().Select(d => d.Content).ToArray(); var anchorables = anchorablesSource as IEnumerable; var listOfAnchorablesToImport = new List<object>(anchorables.OfType<object>()); foreach (var document in listOfAnchorablesToImport.ToArray()) { if (anchorablesImported.Contains(document)) listOfAnchorablesToImport.Remove(document); } LayoutAnchorablePane anchorablePane = null; if (layout.ActiveContent != null) { //look for active content parent pane anchorablePane = layout.ActiveContent.Parent as LayoutAnchorablePane; } if (anchorablePane == null) { //look for a pane on the right side anchorablePane = layout.Descendents().OfType<LayoutAnchorablePane>().Where(pane => !pane.IsHostedInFloatingWindow && pane.GetSide() == AnchorSide.Right).FirstOrDefault(); } if (anchorablePane == null) { //look for an available pane anchorablePane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(); } _suspendLayoutItemCreation = true; foreach (var anchorableContentToImport in listOfAnchorablesToImport) { var anchorableToImport = new LayoutAnchorable() { Content = anchorableContentToImport }; bool added = false; if (LayoutUpdateStrategy != null) { added = LayoutUpdateStrategy.BeforeInsertAnchorable(layout, anchorableToImport, anchorablePane); } if (!added) { if (anchorablePane == null) { var mainLayoutPanel = new LayoutPanel() { Orientation = Orientation.Horizontal }; if (layout.RootPanel != null) { mainLayoutPanel.Children.Add(layout.RootPanel); } layout.RootPanel = mainLayoutPanel; anchorablePane = new LayoutAnchorablePane() { DockWidth = new GridLength(200.0, GridUnitType.Pixel) }; mainLayoutPanel.Children.Add(anchorablePane); } anchorablePane.Children.Add(anchorableToImport); added = true; } if (LayoutUpdateStrategy != null) LayoutUpdateStrategy.AfterInsertAnchorable(layout, anchorableToImport); var anchorableItem = new LayoutAnchorableItem(); anchorableItem.Attach(anchorableToImport); ApplyStyleToLayoutItem(anchorableItem); _layoutItems.Add(anchorableItem); } _suspendLayoutItemCreation = false; var anchorablesSourceAsNotifier = anchorablesSource as INotifyCollectionChanged; if (anchorablesSourceAsNotifier != null) anchorablesSourceAsNotifier.CollectionChanged += new NotifyCollectionChangedEventHandler(anchorablesSourceElementsChanged); }
void anchorablesSourceElementsChanged(object sender, NotifyCollectionChangedEventArgs e) { if (Layout == null) return; //When deserializing documents are created automatically by the deserializer if (SuspendAnchorablesSourceBinding) return; //handle remove if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove || e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace) { if (e.OldItems != null) { var anchorablesToRemove = Layout.Descendents().OfType<LayoutAnchorable>().Where(d => e.OldItems.Contains(d.Content)).ToArray(); foreach (var anchorableToRemove in anchorablesToRemove) { (anchorableToRemove.Parent as ILayoutContainer).RemoveChild( anchorableToRemove); } } } //handle add if (e.NewItems != null && (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add || e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)) { if (e.NewItems != null) { LayoutAnchorablePane anchorablePane = null; if (Layout.ActiveContent != null) { //look for active content parent pane anchorablePane = Layout.ActiveContent.Parent as LayoutAnchorablePane; } if (anchorablePane == null) { //look for a pane on the right side anchorablePane = Layout.Descendents().OfType<LayoutAnchorablePane>().Where(pane => !pane.IsHostedInFloatingWindow && pane.GetSide() == AnchorSide.Right).FirstOrDefault(); } if (anchorablePane == null) { //look for an available pane anchorablePane = Layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(); } _suspendLayoutItemCreation = true; foreach (var anchorableContentToImport in e.NewItems) { var anchorableToImport = new LayoutAnchorable() { Content = anchorableContentToImport }; bool added = false; if (LayoutUpdateStrategy != null) { added = LayoutUpdateStrategy.BeforeInsertAnchorable(Layout, anchorableToImport, anchorablePane); } if (!added) { if (anchorablePane == null) { var mainLayoutPanel = new LayoutPanel() { Orientation = Orientation.Horizontal }; if (Layout.RootPanel != null) { mainLayoutPanel.Children.Add(Layout.RootPanel); } Layout.RootPanel = mainLayoutPanel; anchorablePane = new LayoutAnchorablePane() { DockWidth = new GridLength(200.0, GridUnitType.Pixel) }; mainLayoutPanel.Children.Add(anchorablePane); } anchorablePane.Children.Add(anchorableToImport); added = true; } if (LayoutUpdateStrategy != null) { LayoutUpdateStrategy.AfterInsertAnchorable(Layout, anchorableToImport); } var root = anchorableToImport.Root; if (root != null && root.Manager == this) { var anchorableItem = new LayoutAnchorableItem(); anchorableItem.Attach(anchorableToImport); ApplyStyleToLayoutItem(anchorableItem); _layoutItems.Add(anchorableItem); } } _suspendLayoutItemCreation = false; } } if (e.Action == NotifyCollectionChangedAction.Reset) { //NOTE: I'm going to clear every anchorable present in layout but //some anchorable may have been added directly to the layout, for now I clear them too var anchorablesToRemove = Layout.Descendents().OfType<LayoutAnchorable>().ToArray(); foreach (var anchorableToRemove in anchorablesToRemove) { (anchorableToRemove.Parent as ILayoutContainer).RemoveChild( anchorableToRemove); } } }