// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Collections; public class TreeDataTemplate : DataTemplate { public TreeDataTemplate( Func build, Func itemsSelector) : this(o => true, build, itemsSelector) { } public TreeDataTemplate( Type type, Func build, Func itemsSelector) : this(o => DataTemplate.IsInstance(o, type), build, itemsSelector) { } public TreeDataTemplate( Type type, Func build, Func itemsSelector, Func isExpanded) : this(o => DataTemplate.IsInstance(o, type), build, itemsSelector, isExpanded) { } public TreeDataTemplate( Func match, Func build, Func itemsSelector) : this(match, build, itemsSelector, _ => false) { this.ItemsSelector = itemsSelector; } public TreeDataTemplate( Func match, Func build, Func itemsSelector, Func isExpanded) : base(match, build) { this.ItemsSelector = itemsSelector; this.IsExpanded = isExpanded; } public Func ItemsSelector { get; private set; } public Func IsExpanded { get; private set; } } public class TreeDataTemplate : TreeDataTemplate { public TreeDataTemplate( Func build, Func itemsSelector) : base( typeof(T), TreeDataTemplate.Cast(build), TreeDataTemplate.Cast(itemsSelector)) { } public TreeDataTemplate( Func build, Func itemsSelector, Func isExpanded) : base( typeof(T), TreeDataTemplate.Cast(build), TreeDataTemplate.Cast(itemsSelector), TreeDataTemplate.Cast(isExpanded)) { } public TreeDataTemplate( Func match, Func build, Func itemsSelector) : base( TreeDataTemplate.CastMatch(match), TreeDataTemplate.Cast(build), TreeDataTemplate.Cast(itemsSelector)) { } public TreeDataTemplate( Func match, Func build, Func itemsSelector, Func isExpanded) : base( TreeDataTemplate.CastMatch(match), TreeDataTemplate.Cast(build), TreeDataTemplate.Cast(itemsSelector), TreeDataTemplate.Cast(isExpanded)) { } private static Func CastMatch(Func f) { return o => (o is T) ? f((T)o) : false; } private static Func Cast(Func f) { return o => f((T)o); } } }