public override bool Equals(object other)
        {
            if (!(other is DefaultAttachedPropertyDataDescriptor))
            {
                return(false);
            }
            DefaultAttachedPropertyDataDescriptor mapdd = (DefaultAttachedPropertyDataDescriptor)other;

            return(base.Equals(other) && _propertyProvider.Equals(mapdd._propertyProvider));
        }
 public static bool CreateAttachedPropertyDataDescriptor(INamespaceHandler namespaceHandler, object targetObj,
     string propertyProvider, string propertyName, out DefaultAttachedPropertyDataDescriptor result)
 {
   result = null;
   if (targetObj == null)
     throw new NullReferenceException("Target object 'null' is not supported");
   if (!namespaceHandler.HasAttachedProperty(propertyProvider, propertyName, targetObj))
     return false;
   result = new DefaultAttachedPropertyDataDescriptor(namespaceHandler, targetObj, propertyProvider, propertyName);
   return true;
 }
 public static bool CreateAttachedPropertyDataDescriptor(INamespaceHandler namespaceHandler, object targetObj,
                                                         string propertyProvider, string propertyName, out DefaultAttachedPropertyDataDescriptor result)
 {
     result = null;
     if (targetObj == null)
     {
         throw new NullReferenceException("Target object 'null' is not supported");
     }
     if (!namespaceHandler.HasAttachedProperty(propertyProvider, propertyName, targetObj))
     {
         return(false);
     }
     result = new DefaultAttachedPropertyDataDescriptor(namespaceHandler, targetObj, propertyProvider, propertyName);
     return(true);
 }
Beispiel #4
0
 public bool Evaluate(IDataDescriptor source, out IDataDescriptor result)
 {
     result = null;
     try
     {
         DefaultAttachedPropertyDataDescriptor dapdd;
         if (!DefaultAttachedPropertyDataDescriptor.CreateAttachedPropertyDataDescriptor(_namespaceHandler,
                                                                                         source.Value, _propertyProvider, _propertyName, out dapdd))
         {
             throw new InvalidOperationException(string.Format("Attached property '{0}.{1}' is not available on new target object '{2}'",
                                                               _propertyProvider, _propertyName, source.Value));
         }
         result = dapdd;
     }
     catch (Exception e)
     {
         ServiceRegistration.Get <ILogger>().Warn("AttachedPropertyPathSegment: Cannot evaluate attached property '{0}' on object '{1}'", e, ToString(), source.Value);
         return(false);
     }
     return(true);
 }
Beispiel #5
0
        /// <summary>
        /// Handles nodes for member or event assignment to the currently processed
        /// element in both attribute syntax and member element syntax.
        /// This method ignores attributes from the <c>x:</c> namespace, which
        /// will be handled in method <see cref="CheckNameOrKey"/>.
        /// </summary>
        /// <param name="memberDeclarationNode">Node containing a member or
        /// event assignment for the current element. This node can either
        /// be an <see cref="XmlAttribute"/> node (for attribute syntax) or an
        /// <see cref="XmlElement"/> node (for member element syntax).
        /// Nodes in the <c>x:</c> namespace will be ignored.</param>
        protected void HandleMemberOrEventAssignment(XmlNode memberDeclarationNode)
        {
            ElementContextInfo elementContext = _elementContextStack.CurrentElementContext;
            string             memberName     = memberDeclarationNode.LocalName;
            int    i = memberName.IndexOf('.');
            string explicitTargetQualifier = i == -1 ? string.Empty : memberName.Substring(0, i);

            if (explicitTargetQualifier.Length > 0)
            {
                memberName = memberName.Substring(explicitTargetQualifier.Length + 1); // Cut away target element name
            }
            // Check if we have an attached property
            bool isAttached;

            if (explicitTargetQualifier == string.Empty)
            {
                isAttached = false;
            }
            else if (explicitTargetQualifier == elementContext.Element.LocalName)
            { // Qualifier == element name, maybe an attached property
                string namespaceURI = (memberDeclarationNode.NamespaceURI == string.Empty && memberDeclarationNode is XmlAttribute) ?
                                      _elementContextStack.GetNamespaceOfPrefix(string.Empty) :
                                      memberDeclarationNode.NamespaceURI;
                isAttached = GetNamespaceHandler(namespaceURI).HasAttachedProperty(
                    explicitTargetQualifier, memberName, elementContext.Instance);
            }
            else
            {
                isAttached = true;
            }
            if (isAttached)
            { // Attached property or event attribute
              // For unprefixed attributes, adopt the namespace from the parent node
                string namespaceURI = (memberDeclarationNode.NamespaceURI == string.Empty && memberDeclarationNode is XmlAttribute) ?
                                      _elementContextStack.GetNamespaceOfPrefix(string.Empty) :
                                      memberDeclarationNode.NamespaceURI;
                DefaultAttachedPropertyDataDescriptor dapdd;
                if (!DefaultAttachedPropertyDataDescriptor.CreateAttachedPropertyDataDescriptor(GetNamespaceHandler(namespaceURI),
                                                                                                elementContext.Instance, explicitTargetQualifier, memberName, out dapdd))
                {
                    throw new InvalidOperationException(string.Format("Attached property '{0}.{1}' is not available on new target object '{2}'",
                                                                      explicitTargetQualifier, memberName, elementContext.Instance));
                }
                object value = ParseValue(memberDeclarationNode);
                HandleMemberAssignment(dapdd, value);
            }
            else
            { // Local member
              // We have to check if <c>memberDeclarationNode.Prefix == string.Empty</c>, because
              // unprefixed names normally match the default's namespace URI,
              // but here the namespace URI of those unprefixed nodes implicitly
              // belong to the current element.
                if (memberDeclarationNode.Prefix == string.Empty ||
                    memberDeclarationNode.NamespaceURI == elementContext.Element.NamespaceURI)
                { // Member or event attribute located in the same namespace as the element
                    object value = ParseValue(memberDeclarationNode);
                    Type   t     = elementContext.Instance.GetType();
                    // Search Member data descriptor
                    IDataDescriptor dd;
                    if (ReflectionHelper.FindMemberDescriptor(elementContext.Instance, memberName, out dd))
                    { // Member assignment
                        if (value != null)
                        {
                            HandleMemberAssignment(dd, value);
                        }
                        return;
                    }
                    EventInfo evt = t.GetEvent(memberName);
                    if (evt != null)
                    { // Event assignment
                        HandleEventAssignment(elementContext.Instance, evt, (string)Convert(value, typeof(string)));
                        return;
                    }
                    throw new XamlBindingException("XAML parser: Member '{0}' was not found on type '{1}'",
                                                   memberName, t.Name);
                }
                else if (memberDeclarationNode.NamespaceURI == XamlNamespaceHandler.XAML_NS_URI)
                { // XAML attributes ("x:Attr") will be ignored here - they are evaluated
                  // in the code processing the parent instance
                }
                else
                {
                    throw new XamlParserException("Attribute '{0}' is not defined in namespace '{1}'",
                                                  memberDeclarationNode.LocalName, memberDeclarationNode.NamespaceURI);
                }
            }
        }