Friday, May 8, 2015

Need help implementing DataTemplate Selector.

You'll need to determine if you want the BigCards or the SmallCards template based on the item and then return the appropriate template. How to decide between them is up to the app.

Often a good way to manage the templates is to expose properties for the two templates which the DataTemplateSelector can then return (it looks like you already have those). The properties can be set from Xaml so you don't need to hardcode the actual templates in code-behind.

Set up the ArticleTemplateSelector with the two templates. It can then be set in the control that will use it:

<Page.Resources>
     <local:ArticleTemplateSelector x:Key="articleSelector" LajmetItem="{StaticResource BigCards}" RssItem="{StaticResource SmallCards}" />
</Page.Resources>
And in the code behind choose between the templates based on the item. How to choose is up to the app:
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
   // Determine which template to return;
   if (ShouldUseBigTemplate(item))
   {
       return LajmetItem;
   } 
   else 
   {
       return RssItem;
   }

   return null;
}

No comments:

Post a Comment