// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex.Controls.Templates
{
using System;
using System.Collections;
using System.Reflection;
///
/// A template used to build hierachical data.
///
public class TreeDataTemplate : DataTemplate, ITreeDataTemplate
{
private Func itemsSelector;
private Func isExpanded;
///
/// Initializes a new instance of the class.
///
/// The type of data which the data template matches.
///
/// A function which when passed an object of returns a control.
///
///
/// A function which when passed an object of returns the child
/// items.
///
public TreeDataTemplate(
Type type,
Func build,
Func itemsSelector)
: this(o => IsInstance(o, type), build, itemsSelector)
{
}
///
/// Initializes a new instance of the class.
///
/// The type of data which the data template matches.
///
/// A function which when passed an object of returns a control.
///
///
/// A function which when passed an object of returns the child
/// items.
///
///
/// A function which when passed an object of returns the the
/// initial expanded state of the node.
///
public TreeDataTemplate(
Type type,
Func build,
Func itemsSelector,
Func isExpanded)
: this(o => IsInstance(o, type), build, itemsSelector, isExpanded)
{
}
///
/// Initializes a new instance of the class.
///
///
/// A function which determines whether the data template matches the specified data.
///
///
/// A function which when passed a matching object returns a control.
///
///
/// A function which when passed a matching object returns the child items.
///
public TreeDataTemplate(
Func match,
Func build,
Func itemsSelector)
: this(match, build, itemsSelector, _ => false)
{
this.itemsSelector = itemsSelector;
}
///
/// Initializes a new instance of the class.
///
///
/// A function which determines whether the data template matches the specified data.
///
///
/// A function which when passed a matching object returns a control.
///
///
/// A function which when passed a matching object returns the child items.
///
///
/// A function which when passed a matching object returns the the initial expanded state
/// of the node.
///
public TreeDataTemplate(
Func match,
Func build,
Func itemsSelector,
Func isExpanded)
: base(match, build)
{
this.itemsSelector = itemsSelector;
this.isExpanded = isExpanded;
}
///
/// Checks to see if the item should be initially expanded.
///
/// The item.
/// True if the item should be initially expanded, otherwise false.
public bool IsExpanded(object item)
{
return this?.isExpanded(item) ?? false;
}
///
/// Selects the child items of an item.
///
/// The item.
/// The child items, or null if no child items.
public IEnumerable ItemsSelector(object item)
{
return this?.itemsSelector(item);
}
///
/// Determines of an object is of the specified type.
///
/// The object.
/// The type.
///
/// True if is of type , otherwise false.
///
private static bool IsInstance(object o, Type t)
{
return (o != null) ?
t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) :
false;
}
}
}