Browse Source

Trying to sort out logical tree for ItemsControls.

Not yet complete.

This reverts commit 7a55e20f98.

The reason that searching for template controls needs to search nested
templates is because e.g. an ItemsPresenter may be nested inside a
ScrollViewer.
pull/39/head
Steven Kirk 12 years ago
parent
commit
2417ac1077
  1. 23
      Perspex.Controls/ControlExtensions.cs
  2. 2
      Perspex.Controls/Primitives/SelectingItemsControl.cs
  3. 6
      Perspex.Controls/Primitives/TemplatedControl.cs
  4. 1
      Perspex.Themes.Default/ListBoxStyle.cs
  5. 59
      Tests/Perspex.Controls.UnitTests/ListBoxTests.cs
  6. 1
      Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

23
Perspex.Controls/ControlExtensions.cs

@ -25,21 +25,18 @@ namespace Perspex.Controls
public static IEnumerable<Control> GetTemplateControls(this ITemplatedControl control)
{
return GetTemplateControls(control, (IVisual)control);
}
private static IEnumerable<Control> GetTemplateControls(ITemplatedControl templated, IVisual parent)
{
IVisual visual = parent as IVisual;
var visual = control as IVisual;
foreach (var child in visual.VisualChildren.OfType<Control>().Where(x => x.TemplatedParent == templated))
if (visual != null)
{
yield return child;
foreach (IVisual grandchild in GetTemplateControls(templated, child))
{
yield return (Control)grandchild;
}
return visual.GetVisualDescendents()
.OfType<Control>()
.TakeWhile(x => x.TemplatedParent != null)
.Where(x => x.TemplatedParent == control);
}
else
{
return Enumerable.Empty<Control>();
}
}
}

2
Perspex.Controls/Primitives/SelectingItemsControl.cs

@ -9,9 +9,7 @@ namespace Perspex.Controls.Primitives
using System;
using System.Collections;
using System.Linq;
using Perspex.Controls.Presenters;
using Perspex.Input;
using Perspex.Interactivity;
using Perspex.VisualTree;
public class SelectingItemsControl : ItemsControl

6
Perspex.Controls/Primitives/TemplatedControl.cs

@ -122,11 +122,7 @@ namespace Perspex.Controls.Primitives
this.AddVisualChild(child);
child.Parent = this;
var templateChildren = this.GetVisualDescendents()
.OfType<Control>()
.Where(x => x.TemplatedParent != null);
foreach (var i in templateChildren)
foreach (var i in this.GetTemplateControls())
{
i.ApplyTemplate();
}

1
Perspex.Themes.Default/ListBoxStyle.cs

@ -42,6 +42,7 @@ namespace Perspex.Themes.Default
{
Content = new ItemsPresenter
{
Id = "itemsPresenter",
[~ItemsPresenter.ItemsProperty] = control[~ListBox.ItemsProperty],
[~ItemsPresenter.ItemsPanelProperty] = control[~ListBox.ItemsPanelProperty],
}

59
Tests/Perspex.Controls.UnitTests/ListBoxTests.cs

@ -0,0 +1,59 @@
// -----------------------------------------------------------------------
// <copyright file="ItemsControlTests.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.UnitTests
{
using System;
using System.Linq;
using Perspex.Controls;
using Perspex.Controls.Presenters;
using Perspex.LogicalTree;
using Perspex.Styling;
using Xunit;
public class ListBoxTests
{
[Fact]
public void LogicalChildren_Should_Be_Set()
{
var target = new ListBox
{
Template = new ControlTemplate(x => this.CreateListBoxTemplate(x)),
Items = new[] { "Foo", "Bar", "Baz " },
};
target.ApplyTemplate();
Assert.Equal(3, target.GetLogicalChildren().Count());
foreach (var child in target.GetLogicalChildren())
{
Assert.IsType<ListBoxItem>(child);
}
}
private Control CreateListBoxTemplate(ITemplatedControl parent)
{
return new ScrollViewer
{
Template = new ControlTemplate(x => this.CreateScrollViewerTemplate(x)),
Content = new ItemsPresenter
{
Id = "itemsPresenter",
[~ItemsPresenter.ItemsProperty] = parent.GetObservable(ListBox.ItemsProperty),
}
};
}
private Control CreateScrollViewerTemplate(ITemplatedControl parent)
{
return new ScrollContentPresenter
{
[~ScrollContentPresenter.ContentProperty] = parent.GetObservable(ScrollViewer.ContentProperty),
};
}
}
}

1
Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

@ -65,6 +65,7 @@
</Choose>
<ItemGroup>
<Compile Include="DropDownTests.cs" />
<Compile Include="ListBoxTests.cs" />
<Compile Include="ItemsControlTests.cs" />
<Compile Include="ContentControlTests.cs" />
<Compile Include="DecoratorTests.cs" />

Loading…
Cancel
Save