Browse Source

Display DropDown current selection correctly.

pull/72/merge
Steven Kirk 11 years ago
parent
commit
14c0e17f1b
  1. 11
      Perspex.Controls/Primitives/TemplatedControl.cs
  2. 49
      Tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs

11
Perspex.Controls/Primitives/TemplatedControl.cs

@ -199,16 +199,15 @@ namespace Perspex.Controls.Primitives
var child = this.Template.Build(this);
// We need to call this twice - once before the controls are added to the
// visual tree so that the logical tree can be set up before styling is
// applied.
this.SetTemplatedParentAndApplyChildTemplates(child);
this.AddVisualChild((Visual)child);
// We need to call SetTemplatedParentAndApplyChildTemplates twice - once
// before the controls are added to the visual tree so that the logical
// tree can be set up before styling is applied.
((ISetLogicalParent)child).SetParent(this);
this.SetTemplatedParentAndApplyChildTemplates(child);
// And again after the controls are added to the visual tree, and have their
// styling and thus Template property set.
this.AddVisualChild((Visual)child);
this.SetTemplatedParentAndApplyChildTemplates(child);
this.OnTemplateApplied();

49
Tests/Perspex.Controls.UnitTests/Primitives/TemplatedControlTests.cs

@ -7,6 +7,7 @@
namespace Perspex.Controls.UnitTests.Primitives
{
using System;
using System.Collections.Generic;
using System.Linq;
using Collections;
using Perspex.Controls.Presenters;
@ -81,6 +82,43 @@ namespace Perspex.Controls.UnitTests.Primitives
Assert.True(templatedParents.All(x => x == target));
}
[Fact]
public void Templated_Child_Should_Have_Parent_Set()
{
var target = new TemplatedControl
{
Template = new ControlTemplate(_ => new Decorator())
};
target.ApplyTemplate();
var child = (Decorator)target.GetVisualChildren().Single();
Assert.Equal(target, child.Parent);
Assert.Equal(target, child.GetLogicalParent());
}
[Fact]
public void Templated_Child_Should_Have_ApplyTemplate_Called_With_Logical_Then_Visual_Parent()
{
var target = new TemplatedControl
{
Template = new ControlTemplate(_ => new ApplyTemplateTracker())
};
target.ApplyTemplate();
var child = (ApplyTemplateTracker)target.GetVisualChildren().Single();
Assert.Equal(
new[]
{
new Tuple<IVisual, ILogical>(null, target),
new Tuple<IVisual, ILogical>(target, target),
},
child.Invocations);
}
[Fact]
public void Nested_TemplatedControls_Should_Be_Expanded_And_Have_Correct_TemplatedParent()
{
@ -155,5 +193,16 @@ namespace Perspex.Controls.UnitTests.Primitives
return result;
}
private class ApplyTemplateTracker : Control
{
public List<Tuple<IVisual, ILogical>> Invocations { get; } = new List<Tuple<IVisual, ILogical>>();
public override void ApplyTemplate()
{
base.ApplyTemplate();
this.Invocations.Add(Tuple.Create(this.GetVisualParent(), this.GetLogicalParent()));
}
}
}
}
Loading…
Cancel
Save