3

Im writing my Windows phone 8 app which uses LongListSelector to display some data.

How to set different item template for first and last item in LongListSelector?

Basically I just want to display same information in every item but use little different item "layout" in last and first item.

devha
  • 3,307
  • 4
  • 28
  • 52
  • Any reason to not use the [ListHeader](http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.controls.longlistselector.listheader(v=vs.105).aspx) and [ListFooter](http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.controls.longlistselector.listfooter(v=vs.105).aspx)? – Shawn Kendrot Jul 24 '13 at 19:01

1 Answers1

11

You could implement some kind of data template selector to help in determining which template to select based on index. You can start off by creating a reusable abstract TemplateSelector class. I used many of the ideas explained in Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector but modified implementation to allow for selecting templates based on index instead.

public abstract class TemplateSelector : ContentControl {
  public abstract DataTemplate SelectTemplate(object item, int index, int totalCount, DependencyObject container);

  protected override void OnContentChanged(object oldContent, object newContent) {
    base.OnContentChanged(oldContent, newContent);

    var parent = GetParentByType<LongListSelector>(this);
    var index = parent.ItemsSource.IndexOf(newContent);
    var totalCount = parent.ItemsSource.Count;

    ContentTemplate = SelectTemplate(newContent, index, totalCount, this);
  }

  private static T GetParentByType<T>(DependencyObject element) where T : FrameworkElement {
    T result = null;
    DependencyObject parent = VisualTreeHelper.GetParent(element);

    while (parent != null) {
      result = parent as T;

      if (result != null) {
        return result;
      }

      parent = VisualTreeHelper.GetParent(parent);
    }

    return null;
  }
}

Once you have that class you can add your own data template selector logic. In your case, could be something like this

public class MyTemplateSelector : TemplateSelector {
  public DataTemplate First { get; set; }
  public DataTemplate Default { get; set; }
  public DataTemplate Last { get; set; }

  public override DataTemplate SelectTemplate(object item, int index, int totalCount, DependencyObject container) {
    if (index == 0)
      return First;
    else if (index == totalCount-1)
      return Last;
    else
      return Default;
  }
}

And finally the Xaml

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="first">
        <TextBlock Text="{Binding Name}" Foreground="Yellow" />
    </DataTemplate>
    <DataTemplate x:Key="default">
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>
    <DataTemplate x:Key="last">
        <TextBlock Text="{Binding Name}" Foreground="Red" />
    </DataTemplate>

    <DataTemplate x:Key="SelectingTemplate">
        <local:MyTemplateSelector Content="{Binding}"
                                First="{StaticResource first}"
                                Default="{StaticResource default}"
                                Last="{StaticResource last}"
                                HorizontalContentAlignment="Stretch" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<phone:LongListSelector
            ItemTemplate="{StaticResource SelectingTemplate}"
            ItemsSource="{Binding Data}" />
Alaa Masoud
  • 7,085
  • 3
  • 39
  • 57
  • In some reason I can not get this working... The problem is in SelectingTemplate. If I set the LongListSelector to "manually" use any of the templates (first, default or last) it works fine. Im setting the LongListSelector item source from code behind, could it be the reason?! Currenlty im getting exception with no obvious reason and if I set brake points to MyTemplateSelector, they are not hit.. – devha Jul 24 '13 at 20:14
  • Setting ItemsSource from code behind should also work. What is the exception message you're getting? And at which line? – Alaa Masoud Jul 24 '13 at 21:38
  • The exception what im getting http://pastebin.com/mxULVRuk The exception is caught in my application App.xaml.cs - Application_UnhandledException(...). I can not get any line number, here it is happening... – devha Jul 25 '13 at 14:43
  • Hmm... Did you try make an empty project with just the above code? It is working fine with me. – Alaa Masoud Jul 25 '13 at 16:17
  • Interesting, tried this with empty project, works well. Removed template selector classes from my actual project and make them once again. Works well! Thanks a lot! – devha Jul 25 '13 at 17:11