// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls.Templates
{
using System;
using System.Collections.Generic;
using System.Linq;
using Perspex.Controls;
using Perspex.Styling;
using Perspex.VisualTree;
public static class TemplateExtensions
{
public static IReparentingHost FindReparentingHost(this IControl control)
{
var tp = control.TemplatedParent;
var chain = new List();
while (tp != null)
{
var reparentingHost = tp as IReparentingHost;
var styleable = tp as IStyleable;
if (reparentingHost != null)
{
chain.Add(reparentingHost);
}
tp = styleable?.TemplatedParent ?? null;
}
foreach (var reparenting in chain.AsEnumerable().Reverse())
{
if (reparenting.WillReparentChildrenOf(control))
{
return reparenting;
}
}
return null;
}
public static T FindTemplateChild(this ITemplatedControl control, string id) where T : INamed
{
return control.GetTemplateChildren().OfType().SingleOrDefault(x => x.Name == id);
}
public static T GetTemplateChild(this ITemplatedControl control, string id) where T : INamed
{
var result = control.FindTemplateChild(id);
if (result == null)
{
throw new InvalidOperationException(string.Format(
"Could not find template child '{0}' of type '{1}' in template for '{2}'.",
id,
typeof(T).FullName,
control.GetType().FullName));
}
return result;
}
public static IEnumerable GetTemplateChildren(this ITemplatedControl control)
{
var visual = control as IVisual;
if (visual != null)
{
// TODO: This searches the whole descendent tree - it can stop when it exits the
// template.
return visual.GetVisualDescendents()
.OfType()
.Where(x => x.TemplatedParent == control);
}
else
{
return Enumerable.Empty();
}
}
}
}