Search code examples
windows-phone-8itemtemplatelonglistselector

LongListSelector with items of variable height & content


I've used and am familiar with LongListSelector having a specific item template for all the items listed.

Now I try to implement a more complex LongListSelector where the items added will not be of fixed height/specific content. Could be a couple of textblocks and images in one case and 2 times the same elements in another (it's a set of elements that appears 1,2 or 3 times per item).

Note: I use ObservableCollection which I connect with the LongListSelector.

ObservableCollection<RouteInformation> Routes = new ObservableCollection<RouteInformation>();

        public RoutePage()
        {
            InitializeComponent();

            RoutesLongListSelector.ItemsSource = Routes;
        }

and I populate the list by pulling data from a database and in the end adding them with

Routes.Add(new RouteInformation(..., ...));

Any suggestions?


Solution

  • Define multiple item templates in the page's resources:

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate x:Key="ItemTemplate3Line">...
    </phone:PhoneApplicationPage.Resources>
    
    <LongListSelector x:Name="lls">
    

    and in the code behind depending on your condition, select custom data template:

     if (...)
        lls.ItemTemplate = Resources["ItemTemplate3Line"] as DataTemplate;