A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

86 lines
2.7 KiB

// -----------------------------------------------------------------------
// <copyright file="TemplateExtensions.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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<IReparentingHost>();
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<T>(this ITemplatedControl control, string id) where T : INamed
{
return control.GetTemplateChildren().OfType<T>().SingleOrDefault(x => x.Name == id);
}
public static T GetTemplateChild<T>(this ITemplatedControl control, string id) where T : INamed
{
var result = control.FindTemplateChild<T>(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<Control> 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<Control>()
.Where(x => x.TemplatedParent == control);
}
else
{
return Enumerable.Empty<Control>();
}
}
}
}