Beispiel #1
0
        public static void BindCollection(this VisualTreeElement element, BindableCollectionAttribute attribute, XmlNode node)
        {
            Type         elementType       = element.GetType();
            XmlAttribute templateAttribute = node.Attribute(attribute.TemplateProperty);
            XmlAttribute sourceAttribute   = node.Attribute(attribute.Source);

            if (sourceAttribute == null ^ templateAttribute == null)
            {
                throw new ArgumentException("Bindable collection source or template undefined.");
            }

            if (sourceAttribute == null)
            {
                return;
            }

            PropertyInfo property = elementType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                    .Single(p => p.Name == attribute.TemplateProperty);

            property.SetValue(element, templateAttribute.Value);

            Action <string, dynamic> adder = elementType
                                             .GetMethod(attribute.AdderName)
                                             .CreateDelegate(typeof(Action <string, dynamic>), element)
                                             as Action <string, dynamic>;

            Action <string, dynamic> remover = elementType
                                               .GetMethod(attribute.RemoverName)
                                               .CreateDelegate(typeof(Action <string, dynamic>), element)
                                               as Action <string, dynamic>;

            element.BindingContext.BindCollection(remover, adder, sourceAttribute.BindingExpression());
        }