Question

I have to insert SVG logos as category names on my homepage, each category has its logo. They are defined in the app.xaml as DataTemplates and I am including them on my homepage in a ContentControl with a DataTemplateSelector to display the right logo (the inclusion of the logos work without the template selector but i need it included dinamically).

Here is the xaml on the homepage :

<GroupStyle>
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <Grid Margin="1,0,0,6"  Name="CategoryName">
                <Button AutomationProperties.Name="Group Title" Click="Category_Click" Style="{StaticResource TextPrimaryButtonStyle}">
                    <ContentControl Name="CategoryLogo" Content="{Binding Category.Name}" ContentTemplateSelector="{StaticResource LogoTemplateSelector}" IsHitTestVisible="True" Margin="3,-7,10,10"/>
                </Button>
            </Grid>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
</GroupStyle>

And here is my DataTemplateSelector :

public class LogoTemplateSelector : DataTemplateSelector
{
    public string DefaultTemplateKey { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, Windows.UI.Xaml.DependencyObject container)
    {
        var category = item as String;
        DataTemplate dt = null;

        switch (category)
        {
            case "Category1": dt = FindTemplate(App.Current.Resources, "Logo1");
                break;
            case "Category2": dt = FindTemplate(App.Current.Resources, "Logo2");
                break;
            case "Category3": dt = FindTemplate(App.Current.Resources, "Logo3");
                break;
            case "Category4": dt = FindTemplate(App.Current.Resources, "Logo4");
                break;
            default: dt = FindTemplate(App.Current.Resources, "Logo1");
                break;
        }

        return dt;
    }

    private static DataTemplate FindTemplate(object source, string key)
    {
        var fe = source as FrameworkElement;
        object obj;
        ResourceDictionary rd = fe != null ? fe.Resources : App.Current.Resources;
        if (rd.TryGetValue(key, out obj))
        {
            DataTemplate dt = obj as DataTemplate;
            if (dt != null)
            {
            return dt;
            }
        }
        return null;
    }
}

My problem is that the Content="{Binding Category.Name}" doesn't seem to work because the object item that I get in my DataTemplateSelector is null.

I'm sure that it is supposed to work because at first I had a TextBlock with the same binding and it correctly displayed the category name.

I also tried binding using a style on the ContentControl but it didn't change anything.

Did I do something wrong ?

Thanks

Was it helpful?

Solution

Ok found the answer in the end :

I had to check if my item was null in the template selector

if (category == null)
{
    return null;
}

The DataTemplateSelector is called once before my data is initialized (thus I have no category to bind) and a second time with the categories initialized and binded to my view.

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