예제 #1
0
            internal void Parent(ModelItem parent, ModelItem child)
            {
                if (this._featureManager == null)
                {
                    throw new NotSupportedException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_ParentNotSupported, new object[2]
                    {
                        (object)parent.ItemType.Name,
                        (object)child.ItemType.Name
                    }));
                }
                ParentAdapter parentAdapter1 = (ParentAdapter)null;

                using (IEnumerator <FeatureProvider> enumerator = FeatureExtensions.CreateFeatureProviders(this._featureManager, typeof(ParentAdapter), parent).GetEnumerator())
                {
                    if (enumerator.MoveNext())
                    {
                        ParentAdapter parentAdapter2 = (ParentAdapter)enumerator.Current;
                        ModelItem     parent1        = parentAdapter2.RedirectParent(parent, child.ItemType);
                        if (parent1 == null)
                        {
                            throw new InvalidOperationException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_InvalidRedirectParent, new object[1]
                            {
                                (object)parentAdapter2.GetType().Name
                            }));
                        }
                        if (parent1 != parent)
                        {
                            this.Parent(parent1, child);
                            return;
                        }
                        parentAdapter1 = parentAdapter2;
                    }
                }
                if (parentAdapter1 == null || !parentAdapter1.CanParent(parent, child.ItemType))
                {
                    throw new NotSupportedException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Resources.Error_ParentNotSupported, new object[2]
                    {
                        (object)parent.ItemType.Name,
                        (object)child.ItemType.Name
                    }));
                }
                ModelItem parent2 = child.Parent;

                if (parent2 == parent)
                {
                    return;
                }
                if (parent2 != null)
                {
                    using (IEnumerator <FeatureProvider> enumerator = FeatureExtensions.CreateFeatureProviders(this._featureManager, typeof(ParentAdapter), parent2).GetEnumerator())
                    {
                        if (enumerator.MoveNext())
                        {
                            ((ParentAdapter)enumerator.Current).RemoveParent(parent2, parent, child);
                        }
                    }
                }
                parentAdapter1.Parent(parent, child);
            }
예제 #2
0
        /// <summary>
        /// Parent the given control into the TabControl.
        /// </summary>
        /// <param name="parent">The new parent item for child.</param>
        /// <param name="child">The child item.</param>
        public override void Parent(ModelItem parent, ModelItem child)
        {
            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }
            if (child == null)
            {
                throw new ArgumentNullException("child");
            }

            // Clear existing property values that we don't want to apply to the new container.
            child.Properties[MyPlatformTypes.FrameworkElement.MarginProperty].ClearValue();
            child.Properties[MyPlatformTypes.FrameworkElement.HorizontalAlignmentProperty].ClearValue();
            child.Properties[MyPlatformTypes.FrameworkElement.VerticalAlignmentProperty].ClearValue();

            bool childIsTabItem = child.IsItemOfType(MyPlatformTypes.TabItem.TypeId);

            // We always accept parenting in TabControl if there is not active focused Task.
            // If the control being pasted is not TabItem and TabControl is empty (doesnt have any TabItem(s) in it)
            // then we inject a TabItem with Grid in TabControl and paste the control under consideration in TabItem.
            // We inject a TabItem also in cases when active TabItem is not capable of parenting concerned control
            if (!childIsTabItem)
            {
                // Based on evaluation done in RedirectParent(),
                // if any control other than TabItem is being parented to Control in this Parent() call
                // then we need to add a new Tabitem with Grid in it

                try
                {
                    ModelItem newTabItem = ModelFactory.CreateItem(parent.Context, MyPlatformTypes.TabItem.TypeId, CreateOptions.InitializeDefaults);
                    parent.Content.Collection.Add(newTabItem);

                    // activate the newly added tabItem
                    TabItemDesignModeValueProvider.SetDesignTimeIsSelected(newTabItem, true);

                    int index = parent.Content.Collection.Count - 1;
                    // Find a suitable parent for control to be pasted.
                    // Since we always accept parenting for TabControl when there is no active focused task,
                    // we better make sure this works.
                    AdapterService adapterService = parent.Context.Services.GetService <AdapterService>();
                    if (adapterService != null)
                    {
                        ModelItem targetParent = FindSuitableParent(adapterService, parent, child.ItemType, index);
                        if (targetParent != null)
                        {
                            ParentAdapter parentAdapter = adapterService.GetAdapter <ParentAdapter>(targetParent.ItemType);
                            parentAdapter.Parent(targetParent, child);
                        }
                        else
                        {
                            Debug.Assert(targetParent != null, "Parenting failed!");
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("Parenting Failed", ex.InnerException);
                }
            }
            else
            {
                // child being added is a TabItem;
                child.Properties[MyPlatformTypes.TabItem.IsSelectedProperty].ClearValue();
                parent.Content.Collection.Add(child);
                TabItemDesignModeValueProvider.SetDesignTimeIsSelected(child, true);       // Activate the newly added tabItem
            }
        }