Beispiel #1
0
 public override void Apply(IStyledElement control)
 {
     try
     {
         Property.SetValue(control, Value(control));
     }
     catch (Exception e)
     {
         Logger.TryGet(LogEventLevel.Error, LogArea.Property)?.Log(
             control,
             "Error setting {Property} on {Target}: {Exception}",
             Property.Name,
             control,
             e);
     }
 }
Beispiel #2
0
        private ExpressionObserver CreateElementObserver(
            IStyledElement target,
            string elementName,
            string path,
            bool enableDataValidation)
        {
            Contract.Requires <ArgumentNullException>(target != null);

            var description = $"#{elementName}.{path}";
            var result      = new ExpressionObserver(
                ControlLocator.Track(target, elementName),
                path,
                enableDataValidation,
                description);

            return(result);
        }
        protected ExpressionObserver CreateElementObserver(
            IStyledElement target,
            string elementName,
            ExpressionNode node)
        {
            _ = target ?? throw new ArgumentNullException(nameof(target));

            if (NameScope is null || !NameScope.TryGetTarget(out var scope) || scope is null)
            {
                throw new InvalidOperationException("Name scope is null or was already collected");
            }
            var result = new ExpressionObserver(
                NameScopeLocator.Track(scope, elementName),
                node,
                null);

            return(result);
        }
Beispiel #4
0
        protected ExpressionObserver CreateElementObserver(
            IStyledElement target,
            string elementName,
            ExpressionNode node)
        {
            Contract.Requires <ArgumentNullException>(target != null);

            NameScope.TryGetTarget(out var scope);
            if (scope == null)
            {
                throw new InvalidOperationException("Name scope is null or was already collected");
            }
            var result = new ExpressionObserver(
                NameScopeLocator.Track(scope, elementName),
                node,
                null);

            return(result);
        }
        /// <summary>
        /// Adds a delayed binding to a control.
        /// </summary>
        /// <param name="target">The control.</param>
        /// <param name="property">The property on the control to bind to.</param>
        /// <param name="binding">The binding.</param>
        public static void Add(IStyledElement target, AvaloniaProperty property, IBinding binding)
        {
            if (target.IsInitialized)
            {
                target.Bind(property, binding);
            }
            else
            {
                if (!_entries.TryGetValue(target, out List <Entry> bindings))
                {
                    bindings = new List <Entry>();
                    _entries.Add(target, bindings);

                    // TODO: Make this a weak event listener.
                    target.Initialized += ApplyBindings;
                }

                bindings.Add(new BindingEntry(property, binding));
            }
        }
Beispiel #6
0
        /// <summary>
        /// Adds a delayed value to a control.
        /// </summary>
        /// <param name="target">The control.</param>
        /// <param name="property">The property on the control to bind to.</param>
        /// <param name="value">A function which returns the value.</param>
        public static void Add(IStyledElement target, PropertyInfo property, Func <IStyledElement, object> value)
        {
            if (target.IsInitialized)
            {
                property.SetValue(target, value(target));
            }
            else
            {
                if (!_entries.TryGetValue(target, out var bindings))
                {
                    bindings = new List <Entry>();
                    _entries.Add(target, bindings);

                    // TODO: Make this a weak event listener.
                    target.Initialized += ApplyBindings;
                }

                bindings.Add(new ClrPropertyValueEntry(property, value));
            }
        }
        public void SanityCheck()
        {
            var child        = new Control();
            var parent       = new Decorator();
            var grandParent  = new Border();
            var grandParent2 = new Border();

            IStyledElement currentParent = null;
            var            subscription  = AncestorFinder.Create(child, typeof(Border)).Subscribe(s => currentParent = s);

            Assert.Null(currentParent);
            parent.Child = child;
            Assert.Null(currentParent);
            grandParent.Child = parent;
            Assert.Equal(grandParent, currentParent);
            grandParent.Child  = null;
            grandParent2.Child = parent;
            Assert.Equal(grandParent2, currentParent);

            subscription.Dispose();
            parent.Child = null;
            Assert.Equal(grandParent2, currentParent);
        }
Beispiel #8
0
        private ExpressionObserver CreateFindAncestorObserver(
            IStyledElement target,
            RelativeSource relativeSource,
            string path,
            bool enableDataValidation)
        {
            Contract.Requires <ArgumentNullException>(target != null);

            IObservable <object> controlLocator;

            switch (relativeSource.Tree)
            {
            case TreeType.Logical:
                controlLocator = ControlLocator.Track(
                    (ILogical)target,
                    relativeSource.AncestorLevel - 1,
                    relativeSource.AncestorType);
                break;

            case TreeType.Visual:
                controlLocator = VisualLocator.Track(
                    (IVisual)target,
                    relativeSource.AncestorLevel - 1,
                    relativeSource.AncestorType);
                break;

            default:
                throw new InvalidOperationException("Invalid tree to traverse.");
            }

            return(ExpressionObserverBuilder.Build(
                       controlLocator,
                       path,
                       enableDataValidation,
                       typeResolver: TypeResolver));
        }
 private object GetValue(IStyledElement control, Type targetType)
 {
     return(ColorToBrushConverter.Convert(control.FindResource(ResourceKey), targetType));
 }
 public override void Apply(IStyledElement control)
 {
     control.Bind(Property, Binding);
 }
 public abstract void Apply(IStyledElement control);
Beispiel #12
0
 private object GetValue(IStyledElement control)
 {
     return(control.FindResource(ResourceKey));
 }
Beispiel #13
0
 public ResourceObservable(IStyledElement target, object key)
 {
     _target = target;
     _key    = key;
 }
 public static void SetClasses(IStyledElement element, Classes value)
 {
     element.SetValue(ClassesProperty, value);
     OnClassesChanged(element, value);
 }
 public static Classes GetClasses(IStyledElement element)
 {
     return(element.GetValue(ClassesProperty));
 }
Beispiel #16
0
 /// <summary>
 /// Style
 /// </summary>
 /// <param name="element"></param>
 /// <param name="value"></param>
 public static void SetStyle(IStyledElement element, IStyle value)
 {
     element.SetValue(StyleProperty, value);
     OnStyleChanged(element, value);
 }
Beispiel #17
0
 /// <summary>
 /// Style
 /// </summary>
 /// <param name="element"></param>
 /// <returns></returns>
 public static IStyle GetStyle(IStyledElement element)
 {
     return(element.GetValue(StyleProperty));
 }
Beispiel #18
0
        public BaseItem InsertText(string text, BaseItem afterElement, bool replace)
        {
            BaseItem lastElement = null;

            if (!(afterElement is Paragraph paragraph))
            {
                paragraph = (Paragraph)Service.GetParent(afterElement, typeof(Paragraph));
            }
            if (paragraph == null)
            {
                return(null);
            }
            IStyledElement s = GetStyle(afterElement);

            string[] split = text.Split(new string[] { "\n" }, StringSplitOptions.None);
            if (split.Length == 0)
            {
                return(null);
            }
            text = split[0];
            if (split.Length > 1)
            {
                Service.SeparateItem(paragraph, afterElement);
                Paragraph pp = paragraph;
                for (int i = 1; i < split.Length; i++)
                {
                    Paragraph p = (Paragraph)pp.Clone();
                    if (s.Style is ParagraphStyle)
                    {
                        p.Style = s.Style as ParagraphStyle;
                    }
                    else
                    {
                        p.Style = new ParagraphStyle(this)
                        {
                            ParentStyleName = ((DefaultStyle)s.Style).Name
                        };
                    }
                    p.Clear();
                    List <BaseItem> list = ParseText(afterElement, split[i]);
                    foreach (BaseItem bi in list)
                    {
                        lastElement = bi;
                        p.Add(lastElement);
                    }
                    paragraph.Owner.InsertAfter(pp, p);
                    pp = p;
                }
            }
            List <BaseItem> toReplace = ParseText(afterElement, text);

            for (int i = 0; i < toReplace.Count; i++)
            {
                BaseItem bi = toReplace[i];
                if (lastElement == null)
                {
                    lastElement = bi;
                }
                if (afterElement != paragraph)
                {
                    if (replace && i == 0)
                    {
                        afterElement.Owner.Replace(afterElement, bi);
                    }
                    else
                    {
                        afterElement.Owner.InsertAfter(afterElement, bi);
                    }
                    afterElement = bi;
                }
                else
                {
                    paragraph.Add(bi);
                }
            }
            return(lastElement);
        }
 public ResourceObservable(IStyledElement target, object key, Func <object?, object?>?converter)
 {
     _target    = target;
     _key       = key;
     _converter = converter;
 }
Beispiel #20
0
 public FinderNode(IStyledElement control, Type ancestorType)
 {
     _control      = control;
     _ancestorType = ancestorType;
 }
Beispiel #21
0
 private void OnChildValueChanged(IStyledElement control) => _subject.OnNext(control);
Beispiel #22
0
 public static IDisposable BindClass(this IStyledElement target, string className, IBinding source, object anchor) =>
 ClassBindingManager.Bind(target, className, source, anchor);
Beispiel #23
0
 public static IObservable <T?> Create <T>(IStyledElement control)
     where T : IStyledElement
 {
     return(Create(control, typeof(T)).Select(x => (T?)x));
 }
Beispiel #24
0
 public static IObservable <T> Create <T>(IStyledElement control)
     where T : IStyledElement
 {
     return(Create(control, typeof(T)).Cast <T>());
 }