Browse Source

Tidied up ItemsControl logical parenting a bit.

pull/39/head
Steven Kirk 11 years ago
parent
commit
df08657e71
  1. 1
      Perspex.Controls/Generators/ItemContainerGenerator.cs
  2. 26
      Perspex.Controls/IItemsPanel.cs
  3. 8
      Perspex.Controls/ItemsControl.cs
  4. 31
      Perspex.Controls/Panel.cs
  5. 1
      Perspex.Controls/Perspex.Controls.csproj
  6. 2
      Perspex.Controls/Presenters/ItemsPresenter.cs

1
Perspex.Controls/Generators/ItemContainerGenerator.cs

@ -83,7 +83,6 @@ namespace Perspex.Controls.Generators
foreach (object item in items)
{
Control container = this.CreateContainerOverride(item);
container.Parent = this.Owner;
container.TemplatedParent = null;
this.AddInternal(item, container);
result.Add(container);

26
Perspex.Controls/IItemsPanel.cs

@ -0,0 +1,26 @@
// -----------------------------------------------------------------------
// <copyright file="IItemsPanel.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
/// <summary>
/// Interface used by <see cref="ItemsControl"/> to set logical ownership of the panel's
/// children.
/// </summary>
/// <remarks>
/// <see cref="ItemsControl"/> needs to set the logical parent of each of its items to itself.
/// To do this, it uses this interface to instruct the panel that instead of setting the
/// logical parent for each child to the panel itself, it should set it to that of
/// <see cref="ChildLogicalParent"/>.
/// </remarks>
public interface IItemsPanel
{
/// <summary>
/// Gets or sets the logical parent that should be set on children of the panel.
/// </summary>
ILogical ChildLogicalParent { get; set; }
}
}

8
Perspex.Controls/ItemsControl.cs

@ -69,13 +69,7 @@ namespace Perspex.Controls
{
get
{
if (this.logicalChildren == null)
{
this.logicalChildren = new PerspexReadOnlyListView<IVisual, ILogical>(
new PerspexList<IVisual>(),
x => (ILogical)x);
}
this.ApplyTemplate();
return this.logicalChildren;
}
}

31
Perspex.Controls/Panel.cs

@ -15,10 +15,17 @@ namespace Perspex.Controls
/// <summary>
/// Base class for controls that can contain multiple children.
/// </summary>
public class Panel : Control, ILogical
public class Panel : Control, ILogical, IItemsPanel
{
private Controls children;
private ILogical childLogicalParent;
public Panel()
{
this.childLogicalParent = this;
}
public Controls Children
{
get
@ -58,32 +65,30 @@ namespace Perspex.Controls
}
}
public bool IsLogicalParent { get; set; } = true;
IReadOnlyPerspexList<ILogical> ILogical.LogicalChildren
{
get { return this.children; }
}
ILogical IItemsPanel.ChildLogicalParent
{
get { return this.childLogicalParent; }
set { this.childLogicalParent = value; }
}
private void ClearLogicalParent(IEnumerable<Control> controls)
{
if (this.IsLogicalParent)
foreach (var control in controls)
{
foreach (var control in controls)
{
control.Parent = null;
}
control.Parent = null;
}
}
private void SetLogicalParent(IEnumerable<Control> controls)
{
if (this.IsLogicalParent)
foreach (var control in controls)
{
foreach (var control in controls)
{
control.Parent = this;
}
control.Parent = this.childLogicalParent as Control;
}
}

1
Perspex.Controls/Perspex.Controls.csproj

@ -38,6 +38,7 @@
<Compile Include="Button.cs" />
<Compile Include="DropDown.cs" />
<Compile Include="IContentControl.cs" />
<Compile Include="IItemsPanel.cs" />
<Compile Include="Platform\IPopupImpl.cs" />
<Compile Include="Platform\ITopLevelImpl.cs" />
<Compile Include="Popup.cs" />

2
Perspex.Controls/Presenters/ItemsPresenter.cs

@ -65,7 +65,7 @@ namespace Perspex.Controls.Presenters
{
this.ClearVisualChildren();
this.panel = this.ItemsPanel.Build();
this.panel.IsLogicalParent = false;
((IItemsPanel)this.panel).ChildLogicalParent = this.TemplatedParent as ILogical;
this.AddVisualChild(this.panel);
this.createdPanel = true;
this.ItemsChanged(Tuple.Create(default(IEnumerable), this.Items));

Loading…
Cancel
Save