0

I'm working on a UWP project with a GridView which I would want to dynamically populate with different item templates, depending on the data.

MainPage.xaml:

<GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
          Margin="80, 40, 60, 40" BorderThickness="0"
          ItemsSource="{x:Bind Favorites}"
          ItemTemplateSelector="{StaticResource ItemTemplateSelector}">
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="Margin" Value="0, 0, 0, 32"/>
        </Style>
    </GridView.ItemContainerStyle>
</GridView>

<Page.Resources>
    <local:ItemTemplateSelector x:Key="ItemTemplateSelector"
                                   Template1="{StaticResource Template1}"
                                   Template2="{StaticResource Template2}">

    </local:ItemTemplateSelector>
    <DataTemplate x:Key="Template1" x:DataType="data:Test1" >
        <local:Template1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template1>
    </DataTemplate>
    <DataTemplate x:Key="Template2" x:DataType="data:" >
        <local:Template2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template2>
    </DataTemplate>
</Page.Resources>

ItemTemplateSelector.cs:

public class ItemTemplateSelector: DataTemplateSelector
{
    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        if (item is Test1)
        {
            return Template1;
        }
        else if (item is Test2)
        {
            return Template2;
        }
        else
        {
            return base.SelectTemplateCore(item);
        }
    }

    public DataTemplate Template1 { get; set; }
    public DataTemplate Template2 { get; set; }
}

The issue is that when I try to run the application I get the following error:

Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.
Cannot find a Resource with the Name/Key ItemTemplateSelector [Line: 66 Position: 19]'

1 Answer 1

2

You should define the resource before use. Try to define the Template1 and 2 before the ItemTemplateSelector in page resources. And the resource should define before use.

<Page.Resources>
    <DataTemplate x:Key="Template1" >
        <local:Template1 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template1>
    </DataTemplate>
    <DataTemplate x:Key="Template2"  >
        <local:Template2 HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></local:Template2>
    </DataTemplate>

    <local:ItemTemplateSelector x:Key="ItemTemplateSelector"
                               Template1="{StaticResource Template1}"
                               Template2="{StaticResource Template2}">

    </local:ItemTemplateSelector>
</Page.Resources>

<Grid>
    <GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
      Margin="80, 40, 60, 40" BorderThickness="0"
      ItemTemplateSelector="{StaticResource ItemTemplateSelector}">
        <GridView.ItemContainerStyle>
            <Style TargetType="GridViewItem">
                <Setter Property="Margin" Value="0, 0, 0, 32"/>
            </Style>
        </GridView.ItemContainerStyle>
    </GridView>
</Grid>

I write the code in https://github.com/lindexi/lindexi_gd/commit/ca49c76909fca81fb6518247a732219bb5f0d9a6

4
  • Thanks for your help! Now I'm struggling to display data to the templates... The DataContext in the Template1 is null, so nothing is displayed besides the empty templates. I have this in my user control: this.DataContextChanged += (s, e) => Bindings.Update();
    – Macaret
    Nov 28, 2019 at 10:05
  • You should define the Itemssource of your gridview?!
    – thezapper
    Nov 28, 2019 at 20:23
  • @thezapper in page
    – lindexi
    Nov 29, 2019 at 0:17
  • Did you set die Itemsource in page? Couldn'T find it. Nevertheless I would try, to add this <Grid> <GridView HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="80, 40, 60, 40" BorderThickness="0" ItemsSource="{x:Bind ViewModel.GridViewSource, Mode=OneWay}" ItemTemplateSelector="{StaticResource ItemTemplateSelector}"> <GridView.ItemContainerStyle> <Style TargetType="GridViewItem"> <Setter Property="Margin" Value="0, 0, 0, 32"/> </Style> </GridView.ItemContainerStyle> </GridView> </Grid>
    – thezapper
    Nov 30, 2019 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.