Question

i need help for my WindowsStoreApp using XAML/C#.

Let's start with a very short explanation of my Problem, and if it's not clear enough, then you'll find very detailed information below :)

In short: I have a ContentControl within a GridView. The GridView has the ItemsSource set to a Collection. In my ContentControl i want to use a selfmade DataTemplateSelector. In it's SelectTemplateCore-method i need acces to the items of the GridView's ItemsSource - how can i do that in WinRT?

In detail:

I want to display folders of the user's system, like the Picture's Library, connected USB devices and so on. Different types of folders shall get different icons. Some of the icons i have as XAML Path Data, and some as image files. The folders are stored in a Collection. This Collection (named "Ordner") is stored in the Page's Viewmodel. Each folder has a property (based on enum) that specifies its type. First i use a custom DataTemplateSelector to check whether the folder's type can be displayed via image or via path. It works perfectly.

The code for this is here... I have a Page like this:

<common:LayoutAwarePage [...]
    xmlns:common="using:De.Mms.DiplomSnma.Win8App.Common">
    <common:LayoutAwarePage.Resources>
        <common:OrdnerDataTemplateSelector x:Key="ordnerTemplateSelector"
            TemplatePathquelle="{StaticResource PathquelleDataTemplate}"
            TemplateBildquelle="{StaticResource BildquelleDataTemplate}" />
    </common:LayoutAwarePage.Resources>

    <GridView ItemsSource="{Binding Ordner}" ItemTemplateSelector="{StaticResource ordnerTemplateSelector}" >
        [...]
    </GridView>
</common:LayoutAwarePage>


class OrdnerDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate TemplateBildquelle { get; set; }
    public DataTemplate TemplatePathquelle { get; set; }

    protected override DataTemplate SelectTemplateCore
        (object item, DependencyObject container)
    {
        OrdnerViewModel ordner = item as OrdnerViewModel;
        if (ordner.OrdnerTyp.Equals(EnumClass.OrdnerTyp.Sharepoint))
        {
            return TemplateBildquelle;
        }
        else
        {
            return TemplatePathquelle;
        }
    }
}

If the folder's icon is based on a Path, a DataTemplate called "PathquelleDataTemplate" in a ResourceDictionary is called. Within the DataTemplate is, besides other UI-elements my ContentControl. Here i wanted to use another custom made DataTemplateSelector to check again the folder's type and choose an according DataTemplate, which then contains the correct path and it's data. But of course i could not just use item in the overwritten SelectTemplateCore(item, container). Without any DataContext the item is null. How can i reference the ItemsSource of my (parent-parent-)parent GridView? I'm totally failing with specifying the DataContext. Every piece of help i found is based on WPF and seems not working in WinRT. Or is there a completely different but better suited way to get access to the Collection?

Here is the DataTemplate "PathquelleDataTemplate" (in a ResourceDictionary).

<DataTemplate x:Key="PathquelleDataTemplate">
    <Grid Height="160" Width="220">
        <StackPanel Orientation="Vertical" Background="White">
            [...]
            <StackPanel Orientation="Horizontal">
                <ContentControl ContentTemplateSelector="{StaticResource iconTemplateSelector}" />
                [...]
            </StackPanel>
        </StackPanel>
    </Grid>
</DataTemplate>

And here just as an example one of the DataTemplates the DataTemplateSelector would choose:

<DataTemplate x:Key="BildOrdnerIconCCTemplate">
    <Path Fill="Black" Data="[...ThePathData...]"/>
</DataTemplate>

Thanks in advance for every help and sorry, if my english sounds sometimes a bit strange :)

Was it helpful?

Solution

Set the Content to the DataContext being passed in to PathquelleDataTemplate, i.e.

<ContentControl ContentTemplateSelector="{StaticResource iconTemplateSelector}" Content="{Binding}"/>

As you noted, iconTemplateSelector will get two items, null, then the property. The null is for when the template first is applied and the second is for when the binding is resolved.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top