using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
namespace Itm.Windows.Controls
{
/// <summary>
/// Choses a approperiate data template for the object from the internal collection.
/// </summary>
[ContentProperty("Templates")]
public class DataTypeTemplateSelector : DataTemplateSelector
{
#region Private Static Fields
/// <summary>
/// Collection of available data templates.
/// </summary>
private readonly List<DataTemplate> templates = new List<DataTemplate>();
#endregion
#region Public Properties
/// <summary>
/// Gets the collection of available data templates.
/// </summary>
/// <value>The collection of available data templates.</value>
public List<DataTemplate> Templates
{
get
{
return templates;
}
}
#endregion
#region Public Methods
/// <summary>
/// When overridden in a derived class, returns a <see cref="DataTemplate"/> based on custom logic.
/// </summary>
/// <param name="item">The data object for which to select the template.</param>
/// <param name="container">The data-bound object.</param>
/// <returns>
/// Returns a <see cref="DataTemplate"/> or null. The default value is null.
/// </returns>
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item != null)
{
Type type = item.GetType();
DataTemplate dataTemplate = templates.FirstOrDefault(d => d.DataType == type);
if (dataTemplate != null)
{
return dataTemplate;
}
}
return base.SelectTemplate(item, container);
}
#endregion
}
}