{"id":1705,"date":"2012-08-20T20:02:03","date_gmt":"2012-08-21T01:02:03","guid":{"rendered":"http:\/\/www.wiredprairie.us\/blog\/?p=1705"},"modified":"2012-08-20T20:02:03","modified_gmt":"2012-08-21T01:02:03","slug":"winrtxamlaka-metro-datatemplate-selection-based-on-data-types","status":"publish","type":"post","link":"https:\/\/www.wiredprairie.us\/blog\/index.php\/archives\/1705","title":{"rendered":"WinRT\/Xaml\/AKA Metro DataTemplate selection based on Data Types"},"content":{"rendered":"
You may have noticed that WinRT does not have automatic resolution of a DataTemplate based on the data type of object added to an ItemsControl<\/a>. While unfortunate as this behavior is quite handy, it\u2019s not too difficult to replicate the functionality using a DataTemplateSelector<\/a>.<\/p>\n WPF for example, could do something like this: <\/p>\n <\/div>\n When the Task type as shown above was found in a list, it would have been rendered as a StackPanel<\/a> with three TextBlock<\/a>s automatically. That rocked.<\/p>\n WinRT (Metro\/Xaml, Windows 8 applications) are missing the DataType property of DataTemplates. Yes, some of you might say it\u2019s not missing as it\u2019s V1, but given the heritage of Windows 8 Xaml applications, I consider it missing.<\/p>\n While an exact duplicate of the functionality isn\u2019t possible, it\u2019s relatively simple to get close.<\/p>\n Take the GridView above for example (using the template project in Visual Studio 2012).<\/p>\n I\u2019ve assigned the ItemTemplateSelector to an instance of the TypedTemplateSelector class I\u2019ve created.<\/p>\n In the Resources for the Page, I added a custom DataTemplate:<\/p>\n <\/p>\n I\u2019ve added a DataTemplate with a Key called Type : SampleDataItem<\/strong> (without the spaces though \u2013 they\u2019re auto converted by my WordPress theme to a squiggle face: ).<\/p>\n There\u2019s nothing special about the Template, just the name. It must start with Type:.<\/strong><\/p>\n Here the custom template selector is being constructed in the Resources:<\/p>\n<DataTemplate DataType="{x:Type local:Task}"<\/span>>
<StackPanel>
<TextBlock Text="{Binding Path=TaskName}"<\/span> \/>
<TextBlock Text="{Binding Path=Description}"<\/span>\/>
<TextBlock Text="{Binding Path=Priority}"<\/span>\/>
<\/StackPanel>
<\/DataTemplate><\/pre>\n<<\/span>GridView\n <\/span>x<\/span>:<\/span>Name<\/span>="itemGridView"\n<\/span> <\/span>ItemsSource<\/span>="{<\/span>Binding <\/span>Source<\/span>={<\/span>StaticResource <\/span>groupedItemsViewSource<\/span>}}"\n <\/span>ItemTemplateSelector<\/span>="{<\/span>StaticResource <\/span>typedTemplateSelector<\/span>}"\n <\/span>SelectionMode<\/span>="None"\n <\/span>IsItemClickEnabled<\/span>="True"<\/span>><\/span><\/pre>\n
<\/span><<\/span>local<\/span>:<\/span>TypedTemplateSelector <\/span>x<\/span>:<\/span>Key<\/span>="typedTemplateSelector"\n <\/span>DefaultTemplateKey<\/span>="Standard250x250ItemTemplate" \/>\n<\/<\/span>Page.Resources<\/span>><\/span><\/pre>\n