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);
            }
        }
Esempio n. 2
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);
        }