Exemplo n.º 1
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);

            _context.Services.ExtensionManager.ApplyDesignItemInitializers(item);

            if (!(component is string))
            {
                _sites.Add(component, item);
            }
            if (ComponentRegistered != null)
            {
                ComponentRegistered(this, new DesignItemEventArgs(item));
            }
            return(item);
        }
Exemplo n.º 2
0
            public SetNameAction(XamlDesignItem designItem, string newName)
            {
                this.designItem = designItem;
                this.newName    = newName;

                oldName = designItem.Name;
            }
        void InsertInternal(int index, XamlDesignItem item)
        {
            property.CollectionElements.Insert(index, item.XamlObject);

            if (CollectionChanged != null)
            {
                CollectionChanged(this,
                                  new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
            }

            AddToNamescopeRecursive(item);
        }
        public bool Contains(DesignItem item)
        {
            XamlDesignItem xitem = CheckItemNoException(item);

            if (xitem != null)
            {
                return(property.CollectionElements.Contains(xitem.XamlObject));
            }
            else
            {
                return(false);
            }
        }
        void RemoveInternal(int index, XamlDesignItem item)
        {
            RemoveFromNamescopeRecursive(item);

            Debug.Assert(property.CollectionElements[index] == item.XamlObject);
            property.CollectionElements.RemoveAt(index);

            if (CollectionChanged != null)
            {
                CollectionChanged(this,
                                  new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item, index));
            }
        }
        public int IndexOf(DesignItem item)
        {
            XamlDesignItem xitem = CheckItemNoException(item);

            if (xitem != null)
            {
                return(property.CollectionElements.IndexOf(xitem.XamlObject));
            }
            else
            {
                return(-1);
            }
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 9
0
        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);
            }
        }
        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);
                    }
                }
            }
        }
Exemplo n.º 11
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();
        }
Exemplo n.º 12
0
        /// <summary>
        /// Copy <paramref name="designItems"/> from the designer to clipboard.
        /// </summary>
        public void Cut(ICollection <DesignItem> designItems)
        {
            Clipboard.Clear();
            string cutXaml     = "";
            var    changeGroup = _context.OpenGroup("Cut " + designItems.Count + " elements", designItems);

            foreach (var item in designItems)
            {
                if (item != null && item != _context.RootItem)
                {
                    XamlDesignItem xamlItem = item as XamlDesignItem;
                    if (xamlItem != null)
                    {
                        cutXaml += XamlStaticTools.GetXaml(xamlItem.XamlObject);
                        cutXaml += _delimeter;
                    }
                }
            }
            ModelTools.DeleteComponents(designItems);
            Clipboard.SetText(cutXaml, TextDataFormat.Xaml);
            changeGroup.Commit();
        }
Exemplo n.º 13
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);
            }

            ValueChanged += (x, y) =>
            {
                OnPropertyChanged("Value");
                OnPropertyChanged("ValueOnInstanceOrView");
            };
            ValueOnInstanceChanged += (x, y) =>
            {
                OnPropertyChanged("ValueOnInstance");
                OnPropertyChanged("ValueOnInstanceOrView");
            };
        }
Exemplo n.º 14
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(IComponentPropertyService), new ComponentPropertyService());
            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(IOutlineNodeNameService), new OutlineNodeNameService());
            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());
            }

            EditorManager.SetDefaultTextBoxEditorType(typeof(TextBoxEditor));
            EditorManager.SetDefaultComboBoxEditorType(typeof(ComboBoxEditor));

            // 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.CurrentProjectAssemblyName = loadSettings.CurrentProjectAssemblyName;
            _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);
        }
Exemplo n.º 15
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);

            _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);
        }
Exemplo n.º 16
0
 public XamlModelPropertyCollection(XamlDesignItem item)
 {
     this._item = item;
 }
Exemplo n.º 17
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();
            }
        }
 public RemoveAtAction(XamlModelCollectionElementsCollection collection, int index, XamlDesignItem item)
 {
     this.collection = collection;
     this.index      = index;
     this.item       = item;
 }