-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectors.cs
34 lines (27 loc) · 1.09 KB
/
Selectors.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
public class Selectors
{
/// <summary>
/// Provides a data template selector which honors data templates targeting interfaces implemented by the
/// data context.
/// </summary>
public sealed class InterfaceTemplateSelector : DataTemplateSelector
{
/// <inheritdoc/>
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement containerElement = container as FrameworkElement;
if (null == item || null == containerElement)
return base.SelectTemplate(item, container);
Type itemType = item.GetType();
IEnumerable<Type> dataTypes
= Enumerable.Repeat(itemType, 1).Concat(itemType.GetInterfaces());
DataTemplate template
= dataTypes.Select(t => new DataTemplateKey(t))
.Select(containerElement.TryFindResource)
.OfType<DataTemplate>()
.FirstOrDefault();
return template ?? base.SelectTemplate(item, container);
}
}
}