Inheritance: DesignItem
Example #1
0
        /// <summary>
        /// registers components from an existing XAML tree
        /// </summary>
        internal XamlDesignItem RegisterXamlComponentRecursive(XamlObject obj)
        {
            if (obj == null)
            {
                return(null);
            }

            foreach (XamlProperty prop in obj.Properties)
            {
                RegisterXamlComponentRecursive(prop.PropertyValue as XamlObject);
                foreach (XamlPropertyValue val in prop.CollectionElements)
                {
                    RegisterXamlComponentRecursive(val as XamlObject);
                }
            }

            XamlDesignItem site = new XamlDesignItem(obj, _context);

            _sites.Add(site.Component, site);
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(site));
            }
            return(site);
        }
Example #2
0
        /// <summary>
        /// Copy <paramref name="designItems"/> from the designer to clipboard.
        /// </summary>
        public void Cut(ICollection <DesignItem> designItems)
        {
            Clipboard.Clear();

            var cutList = RemoveChildItemsWhenContainerIsInList(designItems);

            string cutXaml     = "";
            var    changeGroup = _context.OpenGroup("Cut " + cutList.Count + "/" + designItems.Count + " elements", cutList);

            foreach (var item in cutList)
            {
                if (item != null && item != _context.RootItem)
                {
                    XamlDesignItem xamlItem = item as XamlDesignItem;
                    if (xamlItem != null)
                    {
                        cutXaml += XamlStaticTools.GetXaml(xamlItem.XamlObject);
                        cutXaml += _delimeter;
                    }
                }
            }
            ModelTools.DeleteComponents(cutList);
            Clipboard.SetText(cutXaml, TextDataFormat.Xaml);
            changeGroup.Commit();
        }
Example #3
0
            public SetNameAction(XamlDesignItem designItem, string newName)
            {
                this.designItem = designItem;
                this.newName    = newName;

                oldName = designItem.Name;
            }
        public DesignItem RegisterComponentForDesigner(DesignItem parent, object component)
        {
            if (component == null)
            {
                component = new NullExtension();
            }
            else if (component is Type)
            {
                component = new TypeExtension((Type)component);
            }

            XamlObject parentXamlObject = null;

            if (parent != null)
            {
                parentXamlObject = ((XamlDesignItem)parent).XamlObject;
            }

            XamlDesignItem item = new XamlDesignItem(_context.Document.CreateObject(parentXamlObject, component), _context);

            _context.Services.ExtensionManager.ApplyDesignItemInitializers(item);

            if (!(component is string))
            {
                _sites.Add(component, item);
            }
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(item));
            }
            return(item);
        }
        public DesignItem RegisterComponentForDesigner(object component, Type basetype = null)
        {
            if (component == null)
            {
                component = new NullExtension();
            }
            else if (component is Type)
            {
                component = new TypeExtension((Type)component);
            }

            object baseobject = basetype != null && component.GetType() != basetype?CustomInstanceFactory.CreateObjectInstance(basetype, null) : null;

            XamlObject xamlobj = _context.Document.CreateObject(component, baseobject);

            XamlDesignItem item = new XamlDesignItem(xamlobj, _context);

            if (!(component is string))
            {
                _sites.Add(component, item);
            }
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(item));
            }
            return(item);
        }
Example #6
0
        void InsertInternal(int index, XamlDesignItem item)
        {
            property.CollectionElements.Insert(index, item.XamlObject);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
            }
        }
Example #7
0
        void RemoveInternal(int index, XamlDesignItem item)
        {
            Debug.Assert(property.CollectionElements[index] == item.XamlObject);
            property.CollectionElements.RemoveAt(index);

            if (CollectionChanged != null)
            {
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
            }
        }
Example #8
0
        /// <summary>
        /// registers components from an existing XAML tree
        /// </summary>
        internal XamlDesignItem RegisterXamlComponentRecursive(XamlObject obj)
        {
            if (obj == null)
            {
                return(null);
            }

            foreach (XamlProperty prop in obj.Properties)
            {
                RegisterXamlComponentRecursive(prop.PropertyValue as XamlObject);
                foreach (XamlPropertyValue val in prop.CollectionElements)
                {
                    RegisterXamlComponentRecursive(val as XamlObject);
                }
            }

            XamlDesignItem site = new XamlDesignItem(obj, _context);

            _sites.Add(site.Component, site);
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(site));
            }

            if (_context.RootItem != null && !string.IsNullOrEmpty(site.Name))
            {
                var nameScope = _context.RootItem.Component as INameScope;
                nameScope = NameScope.GetNameScope((DependencyObject)_context.RootItem.Component);
                var fnd = nameScope.FindName(site.Name);

                if (fnd != null)
                {
                    string newNm = site.Name + "_Copy";
                    fnd = nameScope.FindName(newNm);
                    if (fnd == null)
                    {
                        site.Name = newNm;
                    }
                    else
                    {
                        int i = 1;
                        while (fnd != null)
                        {
                            newNm = site.Name + "_Copy" + i;
                            fnd   = nameScope.FindName(newNm);
                            i++;
                        }
                        site.Name = newNm;
                    }
                }
            }
            return(site);
        }
Example #9
0
        public XamlModelProperty(XamlDesignItem designItem, XamlProperty property)
        {
            Debug.Assert(designItem != null);
            Debug.Assert(property != null);

            this._designItem = designItem;
            this._property   = property;
            if (property.IsCollection)
            {
                _collectionElements = new XamlModelCollectionElementsCollection(this, property);
            }
        }
        public bool Contains(DesignItem item)
        {
            XamlDesignItem xitem = CheckItemNoException(item);

            if (xitem != null)
            {
                return(property.CollectionElements.Contains(xitem.XamlObject));
            }
            else
            {
                return(false);
            }
        }
        public int IndexOf(DesignItem item)
        {
            XamlDesignItem xitem = CheckItemNoException(item);

            if (xitem != null)
            {
                return(property.CollectionElements.IndexOf(xitem.XamlObject));
            }
            else
            {
                return(-1);
            }
        }
Example #12
0
        public override DesignItem Clone()
        {
            DesignItem     item     = null;
            var            xaml     = XamlStaticTools.GetXaml(this.XamlObject);
            XamlDesignItem rootItem = Context.RootItem as XamlDesignItem;
            var            obj      = XamlParser.ParseSnippet(rootItem.XamlObject, xaml, ((XamlDesignContext)Context).ParserSettings);

            if (obj != null)
            {
                item = ((XamlDesignContext)Context)._componentService.RegisterXamlComponentRecursive(obj);
            }
            return(item);
        }
        XamlDesignItem CheckItem(DesignItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            if (item.Context != modelProperty.DesignItem.Context)
            {
                throw new ArgumentException("The item must belong to the same context as this collection", "item");
            }
            XamlDesignItem xitem = item as XamlDesignItem;

            Debug.Assert(xitem != null);
            return(xitem);
        }
        public override void SetValue(object value)
        {
            XamlPropertyValue newValue;

            if (value == null)
            {
                newValue = _property.ParentObject.OwnerDocument.CreateNullValue();
            }
            else
            {
                XamlComponentService componentService = _designItem.ComponentService;

                XamlDesignItem designItem = value as XamlDesignItem;
                if (designItem == null)
                {
                    designItem = (XamlDesignItem)componentService.GetDesignItem(value);
                }
                if (designItem != null)
                {
                    if (designItem.Parent != null)
                    {
                        throw new DesignerException("Cannot set value to design item that already has a parent");
                    }
                    newValue = designItem.XamlObject;
                }
                else
                {
                    XamlPropertyValue val = _property.ParentObject.OwnerDocument.CreatePropertyValue(value, _property);
                    designItem = componentService.RegisterXamlComponentRecursive(val as XamlObject);
                    newValue   = val;
                }
            }

            UndoService undoService = _designItem.Services.GetService <UndoService>();

            if (undoService != null)
            {
                undoService.Execute(new PropertyChangeAction(this, newValue, true));
            }
            else
            {
                SetValueInternal(newValue);
            }
        }
Example #15
0
		/// <summary>
		/// Copy <paramref name="designItems"/> from the designer to clipboard.
		/// </summary>
		public void Copy(ICollection<DesignItem> designItems)
		{
			Clipboard.Clear();
			string copiedXaml = "";
			var changeGroup = _context.OpenGroup("Copy " + designItems.Count + " elements", designItems);
			foreach (var item in designItems)
			{
				if (item != null)
				{
					XamlDesignItem xamlItem = item as XamlDesignItem;
					if (xamlItem != null) {
						copiedXaml += XamlStaticTools.GetXaml(xamlItem.XamlObject);
						copiedXaml += _delimeter;
					}
				}
			}
			Clipboard.SetText(copiedXaml, TextDataFormat.Xaml);
			changeGroup.Commit();
		}
        private static void AddToNamescopeRecursive(XamlDesignItem designItem)
        {
            NameScopeHelper.NameChanged(designItem.XamlObject, null, designItem.Name);

            foreach (var p in designItem.Properties)
            {
                if (p.Value != null)
                {
                    AddToNamescopeRecursive((XamlDesignItem)p.Value);
                }
                else if (p.IsCollection && p.CollectionElements != null)
                {
                    foreach (var c in p.CollectionElements)
                    {
                        AddToNamescopeRecursive((XamlDesignItem)c);
                    }
                }
            }
        }
Example #17
0
        public DesignItem RegisterComponentForDesigner(object component)
        {
            if (component == null)
            {
                component = new NullExtension();
            }
            else if (component is Type)
            {
                component = new TypeExtension((Type)component);
            }

            XamlDesignItem item = new XamlDesignItem(_context.Document.CreateObject(component), _context);

            _sites.Add(component, item);
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(item));
            }
            return(item);
        }
        public XamlModelProperty(XamlDesignItem designItem, XamlProperty property)
        {
            Debug.Assert(designItem != null);
            Debug.Assert(property != null);

            this._designItem = designItem;
            this._property   = property;
            if (property.IsCollection)
            {
                _collectionElements = new XamlModelCollectionElementsCollection(this, property);
            }

            ValueChanged += (x, y) =>
            {
                OnPropertyChanged("Value");
                OnPropertyChanged("ValueOnInstanceOrView");
            };
            ValueOnInstanceChanged += (x, y) =>
            {
                OnPropertyChanged("ValueOnInstance");
                OnPropertyChanged("ValueOnInstanceOrView");
            };
        }
 void RemoveInternal(int index, XamlDesignItem item)
 {
     Debug.Assert(property.CollectionElements[index] == item.XamlObject);
     property.CollectionElements.RemoveAt(index);
 }
 public XamlModelPropertyCollection(XamlDesignItem item)
 {
     this._item = item;
 }
Example #21
0
			public SetNameAction(XamlDesignItem designItem, string newName)
			{
				this.designItem = designItem;
				this.newName = newName;
				
				oldName = designItem.Name;
			}
		public XamlModelPropertyCollection(XamlDesignItem item)
		{
			this._item = item;
		}
Example #23
0
        /// <summary>
        /// Creates a new XamlDesignContext instance.
        /// </summary>
        public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings)
        {
            if (xamlReader == null)
            {
                throw new ArgumentNullException("xamlReader");
            }
            if (loadSettings == null)
            {
                throw new ArgumentNullException("loadSettings");
            }

            this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService());
            this.Services.AddService(typeof(IToolService), new DefaultToolService(this));
            this.Services.AddService(typeof(UndoService), new UndoService());
            this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this));
            this.Services.AddService(typeof(ViewService), new DefaultViewService(this));
            this.Services.AddService(typeof(OptionService), new OptionService());

            var xamlErrorService = new XamlErrorService();

            this.Services.AddService(typeof(XamlErrorService), xamlErrorService);
            this.Services.AddService(typeof(IXamlErrorSink), xamlErrorService);

            _componentService = new XamlComponentService(this);
            this.Services.AddService(typeof(IComponentService), _componentService);

            foreach (Action <XamlDesignContext> action in loadSettings.CustomServiceRegisterFunctions)
            {
                action(this);
            }

            // register default versions of overridable services:
            if (this.Services.GetService(typeof(ITopLevelWindowService)) == null)
            {
                this.Services.AddService(typeof(ITopLevelWindowService), new WpfTopLevelWindowService());
            }

            // register extensions from the designer assemblies:
            foreach (Assembly designerAssembly in loadSettings.DesignerAssemblies)
            {
                this.Services.ExtensionManager.RegisterAssembly(designerAssembly);
                EditorManager.RegisterAssembly(designerAssembly);
            }

            _parserSettings                        = new XamlParserSettings();
            _parserSettings.TypeFinder             = loadSettings.TypeFinder;
            _parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory;
            _parserSettings.ServiceProvider        = this.Services;
            _doc = XamlParser.Parse(xamlReader, _parserSettings);

            loadSettings.ReportErrors(xamlErrorService);

            if (_doc == null)
            {
                string message;
                if (xamlErrorService != null && xamlErrorService.Errors.Count > 0)
                {
                    message = xamlErrorService.Errors[0].Message;
                }
                else
                {
                    message = "Could not load document.";
                }
                throw new XamlLoadException(message);
            }

            _rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement);

            if (_rootItem != null)
            {
                var rootBehavior = new RootItemBehavior();
                rootBehavior.Intialize(this);
            }


            _xamlEditOperations = new XamlEditOperations(this, _parserSettings);
        }
Example #24
0
		/// <summary>
		/// Creates a new XamlDesignContext instance.
		/// </summary>
		public XamlDesignContext(XmlReader xamlReader, XamlLoadSettings loadSettings)
		{
			if (xamlReader == null)
				throw new ArgumentNullException("xamlReader");
			if (loadSettings == null)
				throw new ArgumentNullException("loadSettings");
			
			this.Services.AddService(typeof(ISelectionService), new DefaultSelectionService());
			this.Services.AddService(typeof(IToolService), new DefaultToolService(this));
			this.Services.AddService(typeof(UndoService), new UndoService());
			this.Services.AddService(typeof(IErrorService), new DefaultErrorService(this));
			this.Services.AddService(typeof(ViewService), new DefaultViewService(this));
			this.Services.AddService(typeof(OptionService), new OptionService());

			var xamlErrorService = new XamlErrorService();
			this.Services.AddService(typeof(XamlErrorService), xamlErrorService);
			this.Services.AddService(typeof(IXamlErrorSink), xamlErrorService);
			
			_componentService = new XamlComponentService(this);
			this.Services.AddService(typeof(IComponentService), _componentService);
			
			foreach (Action<XamlDesignContext> action in loadSettings.CustomServiceRegisterFunctions) {
				action(this);
			}
			
			// register default versions of overridable services:
			if (this.Services.GetService(typeof(ITopLevelWindowService)) == null) {
				this.Services.AddService(typeof(ITopLevelWindowService), new WpfTopLevelWindowService());
			}
			
			// register extensions from the designer assemblies:
			foreach (Assembly designerAssembly in loadSettings.DesignerAssemblies) {
				this.Services.ExtensionManager.RegisterAssembly(designerAssembly);
				EditorManager.RegisterAssembly(designerAssembly);
			}
			
			_parserSettings = new XamlParserSettings();
			_parserSettings.TypeFinder = loadSettings.TypeFinder;
			_parserSettings.CreateInstanceCallback = this.Services.ExtensionManager.CreateInstanceWithCustomInstanceFactory;
			_parserSettings.ServiceProvider = this.Services;
			_doc = XamlParser.Parse(xamlReader, _parserSettings);
			
			loadSettings.ReportErrors(xamlErrorService);
			
			if (_doc == null) {
				string message;
				if (xamlErrorService != null && xamlErrorService.Errors.Count > 0)
					message = xamlErrorService.Errors[0].Message;
				else
					message = "Could not load document.";
				throw new XamlLoadException(message);
			}
			
			_rootItem = _componentService.RegisterXamlComponentRecursive(_doc.RootElement);
			
			if(_rootItem!=null){
				var rootBehavior=new RootItemBehavior();
				rootBehavior.Intialize(this);
			}
				
			
			_xamlEditOperations=new XamlEditOperations(this,_parserSettings);
		}
 public RemoveAtAction(XamlModelCollectionElementsCollection collection, int index, XamlDesignItem item)
 {
     this.collection = collection;
     this.index      = index;
     this.item       = item;
 }
        /// <summary>
        /// registers components from an existing XAML tree
        /// </summary>
        internal XamlDesignItem RegisterXamlComponentRecursive(XamlObject obj)
        {
            if (obj == null)
            {
                return(null);
            }

            foreach (XamlProperty prop in obj.Properties)
            {
                RegisterXamlComponentRecursive(prop.PropertyValue as XamlObject);
                foreach (XamlPropertyValue val in prop.CollectionElements)
                {
                    RegisterXamlComponentRecursive(val as XamlObject);
                }
            }

            XamlDesignItem site = new XamlDesignItem(obj, _context);

            _context.Services.ExtensionManager.ApplyDesignItemInitializers(site);

            _sites.Add(site.Component, site);
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(site));
            }

            if (_context.RootItem != null && !string.IsNullOrEmpty(site.Name))
            {
                var nameScope = NameScopeHelper.GetNameScopeFromObject(((XamlDesignItem)_context.RootItem).XamlObject);

                if (nameScope != null)
                {
                    // The object will be a part of the RootItem namescope, remove local namescope if set
                    NameScopeHelper.ClearNameScopeProperty(obj.Instance);

                    string newName = site.Name;
                    if (nameScope.FindName(newName) != null)
                    {
                        int copyIndex = newName.LastIndexOf("_Copy", StringComparison.Ordinal);
                        if (copyIndex < 0)
                        {
                            newName += "_Copy";
                        }
                        else if (!newName.EndsWith("_Copy", StringComparison.Ordinal))
                        {
                            string copyEnd = newName.Substring(copyIndex + "_Copy".Length);
                            int    copyEndValue;
                            if (Int32.TryParse(copyEnd, out copyEndValue))
                            {
                                newName = newName.Remove(copyIndex + "_Copy".Length);
                            }
                            else
                            {
                                newName += "_Copy";
                            }
                        }

                        int    i = 1;
                        string newNameTemplate = newName;
                        while (nameScope.FindName(newName) != null)
                        {
                            newName = newNameTemplate + i++;
                        }

                        site.Name = newName;
                    }

                    nameScope.RegisterName(newName, obj.Instance);
                }
            }
            return(site);
        }
Example #27
0
        /// <summary>
        /// Paste items from clipboard into the designer.
        /// </summary>
        public void Paste()
        {
            bool   pasted              = false;
            string combinedXaml        = Clipboard.GetText(TextDataFormat.Xaml);
            IEnumerable <string> xamls = combinedXaml.Split(_delimeter);

            xamls = xamls.Where(xaml => xaml != "");

            DesignItem parent = _context.Services.Selection.PrimarySelection;
            DesignItem child  = _context.Services.Selection.PrimarySelection;

            XamlDesignItem rootItem    = _context.RootItem as XamlDesignItem;
            var            pastedItems = new Collection <DesignItem>();

            foreach (var xaml in xamls)
            {
                var obj = XamlParser.ParseSnippet(rootItem.XamlObject, xaml, _settings);
                if (obj != null)
                {
                    DesignItem item = _context._componentService.RegisterXamlComponentRecursive(obj);
                    if (item != null)
                    {
                        pastedItems.Add(item);
                    }
                }
            }

            if (pastedItems.Count != 0)
            {
                var changeGroup = _context.OpenGroup("Paste " + pastedItems.Count + " elements", pastedItems);
                while (parent != null && pasted == false)
                {
                    if (parent.ContentProperty != null)
                    {
                        if (parent.ContentProperty.IsCollection)
                        {
                            if (CollectionSupport.CanCollectionAdd(parent.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)) && parent.GetBehavior <IPlacementBehavior>() != null)
                            {
                                AddInParent(parent, pastedItems);
                                pasted = true;
                            }
                        }
                        else if (pastedItems.Count == 1 && parent.ContentProperty.Value == null && parent.ContentProperty.ValueOnInstance == null && parent.View is ContentControl)
                        {
                            AddInParent(parent, pastedItems);
                            pasted = true;
                        }
                        if (!pasted)
                        {
                            parent = parent.Parent;
                        }
                    }
                    else
                    {
                        parent = parent.Parent;
                    }
                }

                while (pasted == false)
                {
                    if (child.ContentProperty != null)
                    {
                        if (child.ContentProperty.IsCollection)
                        {
                            foreach (var col in child.ContentProperty.CollectionElements)
                            {
                                if (col.ContentProperty != null && col.ContentProperty.IsCollection)
                                {
                                    if (CollectionSupport.CanCollectionAdd(col.ContentProperty.ReturnType, pastedItems.Select(item => item.Component)))
                                    {
                                        pasted = true;
                                    }
                                }
                            }
                            break;
                        }
                        else if (child.ContentProperty.Value != null)
                        {
                            child = child.ContentProperty.Value;
                        }
                        else if (pastedItems.Count == 1)
                        {
                            child.ContentProperty.SetValue(pastedItems.First().Component);
                            pasted = true;
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                foreach (var pastedItem in pastedItems)
                {
                    _context._componentService.RaiseComponentRegisteredAndAddedToContainer(pastedItem);
                }


                changeGroup.Commit();
            }
        }
 void InsertInternal(int index, XamlDesignItem item)
 {
     property.CollectionElements.Insert(index, item.XamlObject);
 }