I create my own UserControl, and I have two different DataTemplates in the UserControl.Resources section in my XAML. I want to choose between these two datasets depending on the value of the property of the objects displayed in the list. I do this by creating my own DataTemplateSelector class and overriding the SelectTemplate method , which should return the DataTemplate that I want to use. However, I have no idea how to "find" my data, which is in the UserControls resources section, all the examples that I saw, extract only data from Window.Resources . In this example, they retrieve the current MainWindow , and then use FindResource to findDataTemplate , how do I get my UserControl > in a similar way ?:
public override DataTemplate
SelectTemplate(object item, DependencyObject container)
{
if (item != null && item is AuctionItem)
{
AuctionItem auctionItem = item as AuctionItem;
Window window = Application.Current.MainWindow;
switch (auctionItem.SpecialFeatures)
{
case SpecialFeatures.None:
return
window.FindResource("AuctionItem_None")
as DataTemplate;
case SpecialFeatures.Color:
return
window.FindResource("AuctionItem_Color")
as DataTemplate;
}
}
return null;
}
An example above from here: ItemsControl.ItemTemplateSelector Property
source
share