Example #1
0
 public static XamlAttachedPropertySetter Create(XamlObjectElement element, Accessors accessors)
 {
     if (accessors == null)
     {
         return(null);
     }
     return(new XamlAttachedPropertySetter(element, accessors));
 }
Example #2
0
        XamlReflectionPropertySetter(XamlObjectElement element, object target, Accessors accessors) : base(element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator())
        {
            this.target    = target;
            this.accessors = accessors;

            if (target is MutableObject)
            {
                is_mutable = true;
            }
        }
Example #3
0
        public void AddToCollection(object value)
        {
            IList list = Accessors.Getter(Element.Object) as IList;

            if (list == null)
            {
                throw Parser.ParseException("Attempt to add attached property to empty list.");
            }

            list.Add(value);
        }
Example #4
0
        public override void SetValue(XamlObjectElement obj, object value)
        {
            var mutable = value as MutableObject;

            if (mutable != null)
            {
                value = mutable.Object;
            }

            if (!typeof(Binding).IsAssignableFrom(Type))
            {
                Binding binding = value as Binding;
                if (binding != null)
                {
                    SetBinding(binding, Element.Object);
                    return;
                }
            }

            if (!typeof(TemplateBindingExpression).IsAssignableFrom(Type))
            {
                TemplateBindingExpression tb = value as TemplateBindingExpression;
                if (tb != null)
                {
                    SetTemplateBinding(tb, obj.Object);
                    return;
                }
            }

            if (value == null || Type.IsAssignableFrom(value.GetType()))
            {
                Accessors.Setter(Element.Object, ConvertValue(Type, value));
                return;
            }

            if (typeof(IList).IsAssignableFrom(Type))
            {
                AddToCollection(value);
                return;
            }

            throw new XamlParseException(
                      string.Format("XamlAttachedPropertySetter.SetValue: Could not set value '{0}' to the attached property '{1}.{2}'",
                                    value,
                                    Accessors.DeclaringType,
                                    Accessors.Name)
                      );
        }
Example #5
0
        private XamlReflectionPropertySetter XamlReflectionPropertyForName(Type target, string name)
        {
            // If there's a normal CLR property of this name, great! We do everything as normal. If there's
            // no CLR property, we need to check for the existence of a DependencyProperty of that name. If
            // the DP exists, we should create a XamlReflectionPropertySetter with a getter/setter that throws
            // an exception. This allows us to do something like: <Rectangle SomeDp={Binding} /> when SomeDp
            // does not have a CLR wrapper property.
            Accessors accessors;
            var       key = new CachedAccessorKey(name, target);

            if (!XamlContext.PropertyAccessorCache.TryGetValue(key, out accessors))
            {
                PropertyInfo       p = target.GetProperty(PropertyName(name), XamlParser.PROPERTY_BINDING_FLAGS);
                DependencyProperty dp;
                if (p != null)
                {
                    accessors = new Accessors(
                        CreateGetter(p.GetGetMethod(true)),
                        CreateSetter(p.GetSetMethod(true)),
                        p.PropertyType,
                        p.Name,
                        Helper.GetConverterCreatorFor(p, p.PropertyType),
                        p.DeclaringType
                        );
                }
                else if (Deployment.Current.MajorVersion >= 4)
                {
                    // The SL4 parser does not allow you to bind to a DP with no CLR wrapper
                    accessors = null;
                }
                else if (DependencyProperty.TryLookup(Deployment.Current.Types.TypeToKind(target), name, out dp) && !dp.IsAttached)
                {
                    accessors = new Accessors(
                        (o) => { throw new XamlParseException(string.Format("The property {0} was not found on element {1}.", name, target.Name)); },
                        (o, a) => { throw new XamlParseException(string.Format("The property {0} was not found on element {1}.", name, target.Name)); },
                        dp.PropertyType,
                        dp.Name,
                        Helper.GetConverterCreatorFor(dp.PropertyType),
                        dp.DeclaringType
                        );
                }

                XamlContext.PropertyAccessorCache.Add(key, accessors);
            }

            return(XamlReflectionPropertySetter.Create(this, Object, accessors));
        }
Example #6
0
		XamlAttachedPropertySetter (XamlObjectElement element, Accessors accessors)
			: base (element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator ())
		{
			Accessors = accessors;
		}
Example #7
0
		public static XamlAttachedPropertySetter Create (XamlObjectElement element, Accessors accessors)
		{
			if (accessors == null)
				return null;
			return new XamlAttachedPropertySetter (element, accessors);
		}
Example #8
0
		XamlReflectionPropertySetter (XamlObjectElement element, object target, Accessors accessors) : base (element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator ())
		{
			this.target = target;
			this.accessors = accessors;

			if (target is MutableObject)
				is_mutable = true;
		}
Example #9
0
		public static XamlReflectionPropertySetter Create (XamlObjectElement element, object target, Accessors accessors)
		{
			if (accessors == null)
				return null;
			return new XamlReflectionPropertySetter (element, target, accessors);
		}
Example #10
0
		private XamlReflectionPropertySetter XamlReflectionPropertyForName (Type target, string name)
		{
			// If there's a normal CLR property of this name, great! We do everything as normal. If there's
			// no CLR property, we need to check for the existence of a DependencyProperty of that name. If
			// the DP exists, we should create a XamlReflectionPropertySetter with a getter/setter that throws
			// an exception. This allows us to do something like: <Rectangle SomeDp={Binding} /> when SomeDp
			// does not have a CLR wrapper property.
			Accessors accessors;
			var key = new CachedAccessorKey (name, target);
			if (!XamlContext.PropertyAccessorCache.TryGetValue (key, out accessors)) {
				PropertyInfo p = target.GetProperty (PropertyName (name), XamlParser.PROPERTY_BINDING_FLAGS);
				DependencyProperty dp;
				if (p != null) {
					accessors = new Accessors (
						CreateGetter (p.GetGetMethod (true)),
						CreateSetter (p.GetSetMethod (true)),
						p.PropertyType,
						p.Name,
						Helper.GetConverterCreatorFor (p, p.PropertyType),
						p.DeclaringType
					);
				} else if (Deployment.Current.MajorVersion >= 4) {
					// The SL4 parser does not allow you to bind to a DP with no CLR wrapper
					accessors = null;
				} else if (DependencyProperty.TryLookup (Deployment.Current.Types.TypeToKind (target), name, out dp) && !dp.IsAttached) {
					accessors = new Accessors (
						(o) => { throw new XamlParseException (string.Format ("The property {0} was not found on element {1}.", name, target.Name)); },
						(o, a) => { throw new XamlParseException (string.Format ("The property {0} was not found on element {1}.", name, target.Name)); },
						dp.PropertyType,
						dp.Name,
						Helper.GetConverterCreatorFor (dp.PropertyType),
						dp.DeclaringType
					);
				}

				XamlContext.PropertyAccessorCache.Add (key, accessors);
			}

			return XamlReflectionPropertySetter.Create (this, Object, accessors);
		}
Example #11
0
 XamlAttachedPropertySetter(XamlObjectElement element, Accessors accessors)
     : base(element, accessors.Name, accessors.ConverterCreator == null ? null : accessors.ConverterCreator())
 {
     Accessors = accessors;
 }
Example #12
0
 public static XamlReflectionPropertySetter Create(XamlObjectElement element, object target, Accessors accessors)
 {
     if (accessors == null)
     {
         return(null);
     }
     return(new XamlReflectionPropertySetter(element, target, accessors));
 }