using System;
using System.Collections;
using Avalonia.Data;
using Avalonia.Reactive;
namespace Avalonia.Controls.Templates
{
///
/// A template used to build hierarchical data.
///
public class FuncTreeDataTemplate : FuncDataTemplate, ITreeDataTemplate
{
private readonly Func _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.
///
public FuncTreeDataTemplate(
Type type,
Func build,
Func itemsSelector)
: this(o => IsInstance(o, type), build, 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.
///
public FuncTreeDataTemplate(
Func match,
Func build,
Func itemsSelector)
: base(match, build)
{
_itemsSelector = itemsSelector;
}
public IDisposable BindChildren(AvaloniaObject target, AvaloniaProperty targetProperty, object item)
{
target.SetCurrentValue(targetProperty, _itemsSelector(item));
return Disposable.Empty;
}
///
/// 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 t.IsInstanceOfType(o);
}
}
}