Browse Source

Correctly apply nested templates.

When a templated control's template itself contains templated controls,
make sure all of the templates are applied, as the nested templated
control might contain something the top-most templated control is
looking for.
pull/12/head
Steven Kirk 12 years ago
parent
commit
01c7d028d0
  1. 4
      Perspex.Controls/ControlExtensions.cs
  2. 2
      Perspex.Controls/ControlTemplate.cs
  3. 2
      Perspex.Controls/Presenters/ContentPresenter.cs
  4. 2
      Perspex.Controls/Presenters/ItemsPresenter.cs
  5. 27
      Perspex.Controls/Primitives/TemplatedControl.cs
  6. 12
      Perspex.Controls/TextBox.cs
  7. 8
      Perspex.Layout/Layoutable.cs

4
Perspex.Controls/ControlExtensions.cs

@ -31,9 +31,9 @@ namespace Perspex.Controls
{
IVisual visual = parent as IVisual;
foreach (IVisual child in visual.VisualChildren.OfType<Control>().Where(x => x.TemplatedParent == templated))
foreach (var child in visual.VisualChildren.OfType<Control>().Where(x => x.TemplatedParent != null))
{
yield return (Control)child;
yield return child;
foreach (IVisual grandchild in GetTemplateControls(templated, child))
{

2
Perspex.Controls/ControlTemplate.cs

@ -7,6 +7,8 @@
namespace Perspex.Controls
{
using System;
using System.Linq;
using Perspex.Layout;
using Perspex.Styling;
public class ControlTemplate

2
Perspex.Controls/Presenters/ContentPresenter.cs

@ -30,7 +30,7 @@ namespace Perspex.Controls.Presenters
set { this.SetValue(ContentProperty, value); }
}
protected override sealed void ApplyTemplate()
public override sealed void ApplyTemplate()
{
if (!this.createdChild)
{

2
Perspex.Controls/Presenters/ItemsPresenter.cs

@ -41,7 +41,7 @@ namespace Perspex.Controls.Presenters
set { this.SetValue(ItemsPanelProperty, value); }
}
protected override sealed void ApplyTemplate()
public override sealed void ApplyTemplate()
{
if (!this.createdPanel)
{

27
Perspex.Controls/Primitives/TemplatedControl.cs

@ -20,6 +20,16 @@ namespace Perspex.Controls.Primitives
private bool templateApplied;
static TemplatedControl()
{
TemplateProperty.Changed.Subscribe(e =>
{
var templatedControl = (TemplatedControl)e.Sender;
templatedControl.templateApplied = false;
templatedControl.InvalidateMeasure();
});
}
public ControlTemplate Template
{
get { return this.GetValue(TemplateProperty); }
@ -30,7 +40,7 @@ namespace Perspex.Controls.Primitives
{
}
protected override void ApplyTemplate()
public sealed override void ApplyTemplate()
{
if (!this.templateApplied)
{
@ -45,6 +55,17 @@ namespace Perspex.Controls.Primitives
var child = this.Template.Build(this);
this.AddVisualChild(child);
var templateChildren = this.GetVisualDescendents()
.OfType<Control>()
.Where(x => x.TemplatedParent != null);
foreach (var i in templateChildren)
{
i.ApplyTemplate();
}
this.OnTemplateApplied();
}
@ -82,7 +103,9 @@ namespace Perspex.Controls.Primitives
protected T FindTemplateChild<T>(string id) where T : Control
{
return this.GetTemplateControls().OfType<T>().FirstOrDefault(x => x.Id == id);
return this.GetTemplateControls()
.Where(x => x.TemplatedParent == this)
.OfType<T>().FirstOrDefault(x => x.Id == id);
}
protected T GetTemplateChild<T>(string id) where T : Control

12
Perspex.Controls/TextBox.cs

@ -141,17 +141,7 @@ namespace Perspex.Controls
protected override void OnTemplateApplied()
{
Decorator textContainer = this.GetVisualDescendents()
.OfType<Decorator>()
.FirstOrDefault(x => x.Id == "textContainer");
if (textContainer == null)
{
throw new Exception(
"TextBox template doesn't contain a textContainer " +
"or textContainer is not a Decorator.");
}
Decorator textContainer = this.GetTemplateChild<Decorator>("textContainer");
textContainer.Content = this.textBoxView = new TextBoxView(this);
}

8
Perspex.Layout/Layoutable.cs

@ -163,6 +163,10 @@ namespace Perspex.Layout
get { return this.previousArrange; }
}
public virtual void ApplyTemplate()
{
}
public void Measure(Size availableSize, bool force = false)
{
if (double.IsNaN(availableSize.Width) || double.IsNaN(availableSize.Height))
@ -259,10 +263,6 @@ namespace Perspex.Layout
property.Changed.Subscribe(AffectsMeasureInvalidate);
}
protected virtual void ApplyTemplate()
{
}
protected virtual void ArrangeCore(Rect finalRect)
{
if (this.IsVisible)

Loading…
Cancel
Save