public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var registry = AvaloniaPropertyRegistry.Instance;
            var parser   = new PropertyParser();
            var reader   = new CharacterReader((string)value);

            var(ns, owner, propertyName) = parser.Parse(reader);
            var ownerType  = TryResolveOwnerByName(context, ns, owner);
            var targetType = context.GetFirstAmbientValue <ControlTemplate>()?.TargetType ??
                             context.GetFirstAmbientValue <Style>()?.Selector?.TargetType ??
                             typeof(Control);
            var effectiveOwner = ownerType ?? targetType;
            var property       = registry.FindRegistered(effectiveOwner, propertyName);

            if (property == null)
            {
                throw new XamlLoadException($"Could not find property '{effectiveOwner.Name}.{propertyName}'.");
            }

            if (effectiveOwner != targetType &&
                !property.IsAttached &&
                !registry.IsRegistered(targetType, property))
            {
                Logger.Warning(
                    LogArea.Property,
                    this,
                    "Property '{Owner}.{Name}' is not registered on '{Type}'.",
                    effectiveOwner,
                    propertyName,
                    targetType);
            }

            return(property);
        }
Beispiel #2
0
        private static object GetDefaultAnchor(ITypeDescriptorContext context)
        {
            // If the target is not a control, so we need to find an anchor that will let us look
            // up named controls and style resources. First look for the closest IControl in
            // the context.
            object anchor = context.GetFirstAmbientValue <IControl>();

            // If a control was not found, then try to find the highest-level style as the XAML
            // file could be a XAML file containing only styles.
            return(anchor ??
                   context.GetService <IRootObjectProvider>()?.RootObject as IStyle ??
                   context.GetLastOrDefaultAmbientValue <IStyle>());
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var(owner, propertyName) = ParseProperty((string)value);
            var ownerType = TryResolveOwnerByName(context, owner) ??
                            context.GetFirstAmbientValue <ControlTemplate>()?.TargetType ??
                            context.GetFirstAmbientValue <Style>()?.Selector?.TargetType;

            if (ownerType == null)
            {
                throw new XamlLoadException(
                          $"Could not determine the owner type for property '{propertyName}'. " +
                          "Please fully qualify the property name or specify a target type on " +
                          "the containing template.");
            }

            var property = AvaloniaPropertyRegistry.Instance.FindRegistered(ownerType, propertyName);

            if (property == null)
            {
                throw new XamlLoadException($"Could not find AvaloniaProperty '{ownerType.Name}.{propertyName}'.");
            }

            return(property);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            var s = (string)value;

            string typeName;
            string propertyName;
            Type   type = null;

            ParseProperty(s, out typeName, out propertyName);

            if (typeName == null)
            {
                var style = context.GetFirstAmbientValue <Style>();

                type = style?.Selector?.TargetType;

                if (type == null)
                {
                    throw new Exception(
                              "Could not determine the target type. Please fully qualify the property name.");
                }
            }
            else
            {
                var typeResolver = context.GetService <IXamlTypeResolver>();
                type = typeResolver.Resolve(typeName);

                if (type == null)
                {
                    throw new Exception($"Could not find type '{typeName}'.");
                }
            }

            // First look for non-attached property on the type and then look for an attached property.
            var property = AvaloniaPropertyRegistry.Instance.FindRegistered(type, s) ??
                           AvaloniaPropertyRegistry.Instance.GetAttached(type)
                           .FirstOrDefault(x => x.Name == propertyName);

            if (property == null)
            {
                throw new Exception(
                          $"Could not find AvaloniaProperty '{type.Name}.{propertyName}'.");
            }

            return(property);
        }