Пример #1
0
 protected void AttachToDataDescriptor(UIElement element)
 {
     if (_element == null)
     {
         return;
     }
     if (!String.IsNullOrEmpty(Property))
     {
         string property = Property;
         int    index    = property.IndexOf('.');
         if (index != -1)
         {
             string propertyProvider = property.Substring(0, index);
             string propertyName     = property.Substring(index + 1);
             DefaultAttachedPropertyDataDescriptor result;
             if (!DefaultAttachedPropertyDataDescriptor.CreateAttachedPropertyDataDescriptor(new MpfNamespaceHandler(),
                                                                                             element, propertyProvider, propertyName, out result))
             {
                 ServiceRegistration.Get <ILogger>().Warn(
                     string.Format("Attached property '{0}' cannot be set on element '{1}'", property, element));
                 return;
             }
             _dataDescriptor = result;
             _dataDescriptor.Attach(OnPropertyChanged);
         }
         else
         {
             if (ReflectionHelper.FindMemberDescriptor(_element, Property, out _dataDescriptor))
             {
                 _dataDescriptor.Attach(OnPropertyChanged);
             }
         }
     }
 }
Пример #2
0
        protected bool FindPropertyDescriptor(UIElement element,
                                              out IDataDescriptor propertyDescriptor, out DependencyObject targetObject)
        {
            string targetName = TargetName;

            propertyDescriptor = null;
            if (string.IsNullOrEmpty(targetName))
            {
                targetObject = element;
            }
            else
            {
                // Search the element in "normal" namescope and in the dynamic structure via the FindElement method
                // I think this is more than WPF does. It makes it possible to find elements instantiated
                // by a template, for example.
                targetObject = element.FindElementInNamescope(targetName) ??
                               element.FindElement(new NameMatcher(targetName));
                if (targetObject == null)
                {
                    return(false);
                }
            }
            string property = Property;
            int    index    = property.IndexOf('.');

            if (index != -1)
            {
                string propertyProvider = property.Substring(0, index);
                string propertyName     = property.Substring(index + 1);
                DefaultAttachedPropertyDataDescriptor result;
                if (!DefaultAttachedPropertyDataDescriptor.CreateAttachedPropertyDataDescriptor(new MpfNamespaceHandler(),
                                                                                                element, propertyProvider, propertyName, out result))
                {
                    ServiceRegistration.Get <ILogger>().Warn(
                        string.Format("Attached property '{0}' cannot be set on element '{1}'", property, targetObject));
                    return(false);
                }
                propertyDescriptor = result;
                return(true);
            }
            else
            {
                string          propertyName = property;
                IDataDescriptor result;
                if (!ReflectionHelper.FindMemberDescriptor(targetObject, propertyName, out result))
                {
                    ServiceRegistration.Get <ILogger>().Warn(
                        string.Format("Property '{0}' cannot be set on element '{1}'", property, targetObject));
                    return(false);
                }
                propertyDescriptor = result;
                return(true);
            }
        }