Search code examples
c#xamlwindows-phone-8.1winrt-xamlwinrt-xaml-toolkit

Cant use ItemTemplateSelector with Flipview


I am trying to use an ItemTemplateSelector with a Flipview the same way i would use an ItemTemplateSelector with a Listview but it does not work and i cant find any examples on how to use one with a Flipview.

Here is my XAML:

<Page.Resources>
    <DataTemplate x:Key="textview">
        <TextBlock Text="{Binding textstring}" FontSize="140"/>
    </DataTemplate>
    <DataTemplate x:Key="imgview">
        <Image Source="{Binding imageurl}" />
    </DataTemplate>
    <local:flipviewselector x:Key="myselector" 
                            imageview="{StaticResource imgview}"
                            stringview="{StaticResource textview}"/>
</Page.Resources>

<FlipView Name="flip"
          ItemsSource="{Binding}"
          ItemTemplateSelector="{StaticResource myselector}"/>

this is my C# code:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        this.NavigationCacheMode = NavigationCacheMode.Required;
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var source = new ObservableCollection<flipviewitem>();
        source.Add(new flipviewitem("test1"));
        source.Add(new flipviewitem("image", new Uri("http://theheightsanimalhospital.com/clients/15389/images/playful-kitten-6683.jpg")));
        source.Add(new flipviewitem("test2"));
        source.Add(new flipviewitem("test3"));
        source.Add(new flipviewitem("test4"));
        flip.DataContext = source;
    }
}
public class flipviewselector : DataTemplateSelector
{
    public DataTemplate imageview { get; set; }
    public DataTemplate stringview { get; set; }

    protected override DataTemplate SelectTemplateCore(object item)
    {
        flipviewitem decide = item as flipviewitem;
        if (decide.imageurl != null)
        {
            return imageview;
        }
        return stringview;
    }
}
public class flipviewitem
{
    public flipviewitem(String text)
    {
        this.textstring = text;
    }
    public flipviewitem(String text, Uri url)
    {
        this.textstring = text;
        this.imageurl = url;
    }
    public String textstring { get; set; }
    public Uri imageurl { get; set; }
}

normal binding to the collection without the ItemTemplateSelector is possible.


Solution

  • After searching more and tring out some things, i found out that the SelectTemplateCore funktion was missing something. It need to look like this:

    protected override DataTemplate SelectTemplateCore(object item,DependencyObject container)
    

    not sure why.