private void AddChildObject(XamlObjectElement obj) { object value = obj.Object; MutableObject mutable = value as MutableObject; if (mutable != null) { value = mutable.Object; } IList list = Object as IList; if (list != null) { list.Add(value); return; } IDictionary dict = Object as IDictionary; if (dict != null) { dict.Add(obj.GetDictionaryKey(), value); return; } XamlReflectionPropertySetter content_property = FindContentProperty(); if (content_property == null) { throw Parser.ParseException("Unable to add element {0} to element {1}.", obj.Name, Name); } content_property.SetValue(value); }
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)); }