Permalink
Browse files

ThingTemplateSelector displays things differently

  • Loading branch information...
druttka committed Jun 30, 2016
1 parent a36d04f commit 1eadbaf3b4547b5a0c51eaa6dae461b194ba14ab
View
@@ -5,4 +5,12 @@
xmlns:local="using:DataTemplates"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:ThingTemplates></local:ThingTemplates>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
@@ -103,6 +103,10 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RedThing.cs" />
<Compile Include="Thing.cs" />
<Compile Include="ThingTemplates.xaml.cs">
<DependentUpon>ThingTemplates.xaml</DependentUpon>
</Compile>
<Compile Include="ThingTemplateSelector.cs" />
</ItemGroup>
<ItemGroup>
<AppxManifest Include="Package.appxmanifest">
@@ -125,6 +129,11 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="ThingTemplates.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Page>
<Page Include="MainPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -10,9 +10,15 @@
<Page.DataContext>
<local:MainPageViewModel x:Name="ViewModel"></local:MainPageViewModel>
</Page.DataContext>
<Page.Resources>
<local:ThingTemplateSelector x:Key="ThingTemplateSelector"></local:ThingTemplateSelector>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{x:Bind ViewModel.Items}">
<ListView
ItemsSource="{x:Bind ViewModel.Items}"
ItemTemplateSelector="{StaticResource ThingTemplateSelector}">
</ListView>
</Grid>
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace DataTemplates
{
public class ThingTemplateSelector : DataTemplateSelector
{
private readonly Dictionary<Type, DataTemplate> TemplatesByType;
private readonly ResourceDictionary Resources;
public ThingTemplateSelector() : this(Application.Current.Resources)
{
}
public ThingTemplateSelector(ResourceDictionary resources)
{
Resources = resources;
TemplatesByType = new Dictionary<Type, DataTemplate>
{
{ typeof(RedThing), resources["RedThingTemplate"] as DataTemplate },
{ typeof(BlueThing), resources["BlueThingTemplate"] as DataTemplate }
};
}
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
DataTemplate dataTemplate;
if (TemplatesByType.TryGetValue(item?.GetType(), out dataTemplate))
{
return dataTemplate;
}
return base.SelectTemplateCore(item, container);
}
}
}
@@ -0,0 +1,17 @@
<ResourceDictionary
x:Class="DataTemplates.ThingTemplates"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:DataTemplates">
<DataTemplate x:Key="RedThingTemplate" x:DataType="local:RedThing">
<Border BorderBrush="Red" BorderThickness="2">
<TextBlock Text="{x:Bind Text}"></TextBlock>
</Border>
</DataTemplate>
<DataTemplate x:Key="BlueThingTemplate" x:DataType="local:BlueThing">
<TextBlock Foreground="Blue" Text="{x:Bind Text}"></TextBlock>
</DataTemplate>
</ResourceDictionary>
@@ -0,0 +1,10 @@
namespace DataTemplates
{
public partial class ThingTemplates
{
public ThingTemplates()
{
this.InitializeComponent();
}
}
}

0 comments on commit 1eadbaf

Please sign in to comment.