Exemple #1
0
        private void Branch(DropZoneLocation location, DragablzItem sourceDragablzItem)
        {
            if (InterLayoutClient == null)
            {
                throw new InvalidOperationException("InterLayoutClient is not set.");
            }

            var sourceOfDragItemsControl = ItemsControl.ItemsControlFromItemContainer(sourceDragablzItem) as DragablzItemsControl;

            if (sourceOfDragItemsControl == null)
            {
                throw new ApplicationException("Unable to determin source items control.");
            }

            var sourceTabControl = TabablzControl.GetOwnerOfHeaderItems(sourceOfDragItemsControl);

            if (sourceTabControl == null)
            {
                throw new ApplicationException("Unable to determin source tab control.");
            }

            var sourceItem = sourceOfDragItemsControl.ItemContainerGenerator.ItemFromContainer(sourceDragablzItem);

            sourceTabControl.RemoveItem(sourceDragablzItem);

            var branchItem = new Branch
            {
                Orientation = (location == DropZoneLocation.Right || location == DropZoneLocation.Left) ? Orientation.Horizontal : Orientation.Vertical
            };

            object newContent;

            if (BranchTemplate == null)
            {
                var newTabHost = InterLayoutClient.GetNewHost(Partition, sourceTabControl);
                if (newTabHost == null)
                {
                    throw new ApplicationException("InterLayoutClient did not provide a new tab host.");
                }
                newTabHost.TabablzControl.AddToSource(sourceItem);
                newTabHost.TabablzControl.SelectedItem = sourceItem;
                newContent = newTabHost.Container;
            }
            else
            {
                newContent = new ContentControl
                {
                    Content         = new object(),
                    ContentTemplate = BranchTemplate,
                };
                ((ContentControl)newContent).Dispatcher.BeginInvoke(new Action(() =>
                {
                    //TODO might need to improve this a bit, make it a bit more declarative for complex trees
                    var newTabControl = ((ContentControl)newContent).VisualTreeDepthFirstTraversal().OfType <TabablzControl>().FirstOrDefault();
                    if (newTabControl != null)
                    {
                        newTabControl.DataContext = sourceTabControl.DataContext;
                        newTabControl.AddToSource(sourceItem);
                        newTabControl.SelectedItem = sourceItem;
                    }
                }), DispatcherPriority.Loaded);
            }

            if (location == DropZoneLocation.Right || location == DropZoneLocation.Bottom)
            {
                branchItem.FirstItem  = Content;
                branchItem.SecondItem = newContent;
            }
            else
            {
                branchItem.FirstItem  = newContent;
                branchItem.SecondItem = Content;
            }

            SetCurrentValue(ContentProperty, branchItem);
        }
        private void Branch(DropZoneLocation location, DockItem sourceDockItem)
        {
            if (InterLayoutClient == null)
            {
                throw new InvalidOperationException("InterLayoutClient is not set.");
            }

            var sourceOfdockItemsControl =
                sourceDockItem.GetSelfAndLogicalAncestors().OfType <ItemsControl>().FirstOrDefault() as DockItemsControl;

            if (sourceOfdockItemsControl == null)
            {
                return;
            }

            var sourceDockControl = DockControl.GetOwnerOfHeaderItems(sourceOfdockItemsControl);

            if (sourceDockControl == null)
            {
                throw new ApplicationException("Unable to determine source dock control ");
            }

            var floatingItemSnapShots = sourceDockControl.GetVisualChildren().OfType <Layout>()
                                        .SelectMany(l => l.FloatingDockItems().Select(FloatingItemSnapShot.Take))
                                        .ToList();

            var sourceItemIndex = sourceOfdockItemsControl.ItemContainerGenerator.IndexFromContainer(sourceDockItem);
            var sourceItem      = (DockItem)((IList)sourceDockControl.Items)[sourceItemIndex];

            sourceDockControl.RemoveItem(sourceItem);

            var branchItem = new Branch
            {
                Orientation = (location == DropZoneLocation.Right || location == DropZoneLocation.Left) ? Orientation.Horizontal : Orientation.Vertical,
            };

            object newContent;

            if (BranchTemplate == null)
            {
                var newTabHost = InterLayoutClient.GetNewHost(Partition, sourceDockControl);
                if (newTabHost == null)
                {
                    throw new ApplicationException("InterLayoutClient did not provide a new tab host.");
                }
                newTabHost.DockControl.AddToSource(sourceItem);
                newTabHost.DockControl.SelectedItem = sourceItem;
                newContent = newTabHost.Container;

                Dispatcher.UIThread.InvokeAsync(new Action(() => RestoreFloatingItemSnapShots(newTabHost.DockControl, floatingItemSnapShots)), DispatcherPriority.Loaded);
            }
            else
            {
                newContent = new ContentControl
                {
                    Content = new object(),
                };

                ((ContentControl)newContent).DataTemplates.Add(BranchTemplate);

                Dispatcher.UIThread.InvokeAsync(new Action(() =>
                {
                    var newDockControl = ((ContentControl)newContent).GetVisualChildren().OfType <DockControl>().FirstOrDefault();
                    if (newDockControl == null)
                    {
                        return;
                    }

                    newDockControl.DataContext = sourceDockControl.DataContext;
                    newDockControl.AddToSource(sourceItem);
                    newDockControl.SelectedItem = sourceItem;
                    Dispatcher.UIThread.InvokeAsync(new Action(() => RestoreFloatingItemSnapShots(newDockControl, floatingItemSnapShots)), DispatcherPriority.Loaded);
                }), DispatcherPriority.Loaded);
            }

            if (location == DropZoneLocation.Right || location == DropZoneLocation.Bottom)
            {
                branchItem.FirstItem  = Content;
                branchItem.SecondItem = newContent;
            }
            else
            {
                branchItem.FirstItem  = newContent;
                branchItem.SecondItem = Content;
            }

            SetValue(ContentProperty, branchItem);
        }