Example #1
0
        /// <summary>
        /// Create a XamlPropertyValue for the specified value instance.
        /// </summary>
        public XamlPropertyValue CreatePropertyValue(object instance, XamlProperty forProperty)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            Type          elementType = instance.GetType();
            TypeConverter c           = TypeDescriptor.GetConverter(instance);
            var           ctx         = new DummyTypeDescriptorContext(this.ServiceProvider);

            ctx.Instance = instance;
            bool hasStringConverter = c.CanConvertTo(ctx, typeof(string)) && c.CanConvertFrom(typeof(string));

            if (forProperty != null && hasStringConverter)
            {
                return(new XamlTextValue(this, c.ConvertToInvariantString(ctx, instance)));
            }

            string ns     = GetNamespaceFor(elementType);
            string prefix = GetPrefixForNamespace(ns);

            XmlElement xml = _xmlDoc.CreateElement(prefix, elementType.Name, ns);

            if (hasStringConverter &&
                XamlObject.GetContentPropertyName(elementType) != null)
            {
                xml.InnerText = c.ConvertToInvariantString(instance);
            }

            return(new XamlObject(this, xml, elementType, instance));
        }
Example #2
0
        /// <summary>
        /// Create a XamlPropertyValue for the specified value instance.
        /// </summary>
        public XamlPropertyValue CreatePropertyValue(object instance, XamlProperty forProperty)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            Type          elementType = instance.GetType();
            TypeConverter c           = TypeDescriptor.GetConverter(instance);
            var           ctx         = new DummyTypeDescriptorContext(this.ServiceProvider);

            ctx.Instance = instance;
            bool hasStringConverter = c.CanConvertTo(ctx, typeof(string)) && c.CanConvertFrom(typeof(string));

            if (forProperty != null && hasStringConverter)
            {
                return(new XamlTextValue(this, c.ConvertToInvariantString(ctx, instance)));
            }

            string ns     = GetNamespaceFor(elementType);
            string prefix = GetPrefixForNamespace(ns);

            XmlElement xml = _xmlDoc.CreateElement(prefix, elementType.Name, ns);

            if (hasStringConverter && XamlObject.GetContentPropertyName(elementType) != null)
            {
                xml.InnerText = c.ConvertToInvariantString(instance);
            }
            else if (instance is Brush && forProperty != null)                  // TODO: this is a hacky fix, because Brush Editor doesn't
            // edit Design Items and so we have no XML, only the Brush
            // object and we need to parse the Brush to XAML!
            {
                var s = new MemoryStream();
                XamlWriter.Save(instance, s);
                s.Seek(0, SeekOrigin.Begin);
                XmlDocument doc = new XmlDocument();
                doc.Load(s);
                xml = (XmlElement)_xmlDoc.ImportNode(doc.DocumentElement, true);

                var attLst = xml.Attributes.Cast <XmlAttribute>().ToList();
                foreach (XmlAttribute att in attLst)
                {
                    if (att.Name.StartsWith(XamlConstants.Xmlns))
                    {
                        var rootAtt = doc.DocumentElement.GetAttributeNode(att.Name);
                        if (rootAtt != null && rootAtt.Value == att.Value)
                        {
                            xml.Attributes.Remove(att);
                        }
                    }
                }
            }
            else if (instance is string)
            {
                xml.InnerText = (string)instance;
            }

            return(new XamlObject(this, xml, elementType, instance));
        }
Example #3
0
		/// <summary>
		/// Create a XamlPropertyValue for the specified value instance.
		/// </summary>
		public XamlPropertyValue CreatePropertyValue(object instance, XamlProperty forProperty)
		{
			if (instance == null)
				throw new ArgumentNullException("instance");
			
			Type elementType = instance.GetType();
			TypeConverter c = TypeDescriptor.GetConverter(instance);
			var ctx = new DummyTypeDescriptorContext(this.ServiceProvider);
			ctx.Instance = instance;
			bool hasStringConverter = c.CanConvertTo(ctx, typeof(string)) && c.CanConvertFrom(typeof(string));
			if (forProperty != null && hasStringConverter) {
				return new XamlTextValue(this, c.ConvertToInvariantString(ctx, instance));
			}

			string ns = GetNamespaceFor(elementType);
			string prefix = GetPrefixForNamespace(ns);
			
			XmlElement xml = _xmlDoc.CreateElement(prefix, elementType.Name, ns);

			if (hasStringConverter && XamlObject.GetContentPropertyName(elementType) != null) {
				xml.InnerText = c.ConvertToInvariantString(instance);
			} else if (instance is Brush && forProperty != null) {  // TODO: this is a hacky fix, because Brush Editor doesn't
										     // edit Design Items and so we have no XML, only the Brush 
										     // object and we need to parse the Brush to XAML!
				var s = new MemoryStream();
				XamlWriter.Save(instance, s);
				s.Seek(0, SeekOrigin.Begin);
				XmlDocument doc = new XmlDocument();
				doc.Load(s);
				xml = (XmlElement)_xmlDoc.ImportNode(doc.DocumentElement, true);

				var attLst = xml.Attributes.Cast<XmlAttribute>().ToList();
				foreach (XmlAttribute att in attLst) {
					if (att.Name.StartsWith(XamlConstants.Xmlns)) {
						var rootAtt = doc.DocumentElement.GetAttributeNode(att.Name);
						if (rootAtt != null && rootAtt.Value == att.Value) {
							xml.Attributes.Remove(att);
						}
					}
				}
			} else if (instance is string) {
				xml.InnerText = (string)instance;
			}

			return new XamlObject(this, xml, elementType, instance);
		}
Example #4
0
		/// <summary>
		/// Create a XamlPropertyValue for the specified value instance.
		/// </summary>
		public XamlPropertyValue CreatePropertyValue(object instance, XamlProperty forProperty)
		{
			if (instance == null)
				throw new ArgumentNullException("instance");
			
			Type elementType = instance.GetType();
			TypeConverter c = TypeDescriptor.GetConverter(instance);
			var ctx = new DummyTypeDescriptorContext(this.ServiceProvider);
			ctx.Instance = instance;
			bool hasStringConverter = c.CanConvertTo(ctx, typeof(string)) && c.CanConvertFrom(typeof(string));
			if (forProperty != null && hasStringConverter) {
				return new XamlTextValue(this, c.ConvertToInvariantString(ctx, instance));
			}
			
			
			XmlElement xml = _xmlDoc.CreateElement(elementType.Name, GetNamespaceFor(elementType));
			
			if (hasStringConverter) {
				xml.InnerText = c.ConvertToInvariantString(instance);
			}
			return new XamlObject(this, xml, elementType, instance);
		}