Exemplo n.º 1
0
        /// <inheritdoc/>
        public sealed override void ApplyTemplate()
        {
            var template = Template;
            var logical  = (ILogical)this;

            // Apply the template if it is not the same as the template already applied - except
            // for in the case that the template is null and we're not attached to the logical
            // tree. In that case, the template has probably been cleared because the style setting
            // the template has been detached, so we want to wait until it's re-attached to the
            // logical tree as if it's re-attached to the same tree the template will be the same
            // and we don't need to do anything.
            if (_appliedTemplate != template && (template != null || logical.IsAttachedToLogicalTree))
            {
                if (VisualChildren.Count > 0)
                {
                    foreach (var child in this.GetTemplateChildren())
                    {
                        child.SetValue(TemplatedParentProperty, null);
                        ((ISetLogicalParent)child).SetParent(null);
                    }

                    VisualChildren.Clear();
                }

                if (template != null)
                {
                    Logger.TryGet(LogEventLevel.Verbose, LogArea.Control)?.Log(this, "Creating control template");

                    var(child, nameScope) = template.Build(this);
                    ApplyTemplatedParent(child);
                    ((ISetLogicalParent)child).SetParent(this);
                    VisualChildren.Add(child);

                    // Existing code kinda expect to see a NameScope even if it's empty
                    if (nameScope == null)
                    {
                        nameScope = new NameScope();
                    }

                    var e = new TemplateAppliedEventArgs(nameScope);
                    OnApplyTemplate(e);
                    OnTemplateApplied(e);
                    RaiseEvent(e);
                }

                _appliedTemplate = template;
            }
        }
Exemplo n.º 2
0
 public void SetControlTemplate <TThis>(IControlTemplate <UIElement, TThis> template)
     where TThis : Control
 {
     if (this is TThis me)
     {
         if (template == null)
         {
             this.templateInstanciator = null;
         }
         else
         {
             this.templateInstanciator = () => template.InstanciateObject(me);
         }
         DisplayContent = GetControlTemplatedInstance();
     }
     else
     {
         throw new ArgumentException($"{nameof(TThis)} must be of type {this.GetType().FullName} or assinable to it.", nameof(TThis));
     }
 }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public override sealed void ApplyTemplate()
        {
            var template = Template;
            var logical = (ILogical)this;

            // Apply the template if it is not the same as the template already applied - except
            // for in the case that the template is null and we're not attached to the logical
            // tree. In that case, the template has probably been cleared because the style setting
            // the template has been detached, so we want to wait until it's re-attached to the
            // logical tree as if it's re-attached to the same tree the template will be the same
            // and we don't need to do anything.
            if (_appliedTemplate != template && (template != null || logical.IsAttachedToLogicalTree))
            {
                if (VisualChildren.Count > 0)
                {
                    foreach (var child in this.GetTemplateChildren())
                    {
                        child.SetValue(TemplatedParentProperty, null);
                    }

                    VisualChildren.Clear();
                }

                if (template != null)
                {
                    Logger.Verbose(LogArea.Control, this, "Creating control template");

                    var child = template.Build(this);
                    var nameScope = new NameScope();
                    NameScope.SetNameScope((Control)child, nameScope);
                    child.SetValue(TemplatedParentProperty, this);
                    RegisterNames(child, nameScope);
                    ((ISetLogicalParent)child).SetParent(this);
                    VisualChildren.Add(child);

                    OnTemplateApplied(new TemplateAppliedEventArgs(nameScope));
                }

                _appliedTemplate = template;
            }
        }
Exemplo n.º 4
0
 public static void SetIcon(IAvaloniaObject obj, IControlTemplate value)
 {
     obj.SetValue(IconProperty, value);
 }
Exemplo n.º 5
0
 public static void SetDefaultControlTemplate <TReturnType, TTemplatedParent>(IControlTemplate <TReturnType, TTemplatedParent> controlTemplate)
     where TTemplatedParent : UI.Controls.Control
 {
     controlTemplates.Add(typeof(TTemplatedParent), controlTemplate);
 }
Exemplo n.º 6
0
 public static void SetControlTemplate <TReturnType, TTemplatedParent>(this TTemplatedParent parent, IControlTemplate <TReturnType, TTemplatedParent> template)
     where TTemplatedParent : UI.Controls.Control
     where TReturnType : UI.UIElement
 {
     parent.SetControlTemplate <TTemplatedParent>(template);
 }