Enhancing Xamarin.Forms ListView with Grouping Headers: Bind itemTemplate to whole List

AdrnHAdrnH USMember ✭✭

@JamesMontemagno

I need to bind a whole List which I grouped by a string, following JamesMontemagnos exmaple, to a ItemTemplate.
What is the right key word to bind this property of an ObservableCollection. By now I tried:

<ListView.ItemTemplate> <DataTemplate> <cells:SentenceCell List="{Binding Items}"/> </DataTemplate> </ListView.ItemTemplate>

As u see I am using a Custom Renderer for my ViewCell which is capable of binding a list.

James Montemagno's example:
motzcod.es/post/94643411707/enhancing-xamarinforms-listview-with-grouping

Best Answer

Answers

  • AdrnHAdrnH USMember ✭✭
    edited January 2017

    Now I did:

    <cells:SentenceCell List="{Binding}"/>

    Which led to:

    Binding: Entities.Item can not be converted to type 'System.Collections.ObjectModel.ObservableCollection``1[Entities.Item]'

    I guess I would have to augment my linq query where i group my sentences now.

    var sorted = from itemin itemList orderby item.position group itemby item.Title into itemGroup select new Grouping<string, Item>(itemGroup.Key, itemGroup); ItemListSorted = new CustomObservableCollection<Grouping<string, Item>>(sorted);

    into something like:

    var sorted = from itemin itemList orderby item.position group item by item.Title into itemGroup select new Grouping<string, List<Item>>(itemGroup.Key, itemGroup.ToList()); ItemListSorted = new CustomObservableCollection<Grouping<string, List<Item>>>(sorted);

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    YOu would need something like:

      <ListView 
                ItemsSource="{Binding ItemListSorted}" 
                IsGroupingEnabled="True"
                GroupDisplayBinding="{Binding Key}"
                HasUnevenRows="True">
    
  • AdrnHAdrnH USMember ✭✭

    Hey @JamesMontemagno

    That's true and alright.
    My whole ListView looks like this:

    ViewPage

    <ListView x:Name="ItemListSortedListView" ItemsSource="{Binding ItemListSorted}" IsGroupingEnabled="True" GroupDisplayBinding="{Binding Key}" GroupShortNameBinding="{Binding Key}" ItemTapped="OnSentenceSelected" SeparatorVisibility="None" SeparatorColor="Transparent" HasUnevenRows="True">
    <ListView.GroupHeaderTemplate> <DataTemplate> <ViewCell> <StackLayout Margin="0,15,0,5" BackgroundColor="Transparent"> <Label Text="{Binding Key}" TextColor="{x:Static res:Colors.text}" FontSize="{Binding BindingContext.HeaderSize, Source={x:Reference ItemsListSortedListView}}" FontAttributes="Bold" InputTransparent="true" LineBreakMode="TailTruncation"/> </StackLayout> </ViewCell> </DataTemplate> </ListView.GroupHeaderTemplate>

    <ListView.ItemTemplate> <DataTemplate> <cells:ItemViewCell List="{Binding}"/> </DataTemplate> </ListView.ItemTemplate> </ListView>

    with ItemViewCell

    public class ItemCell : ViewCell { public static readonly BindableProperty ListProperty = BindableProperty.Create("List", typeof(IEnumerable<Sentence>), typeof(SentenceCell)); public IEnumerable <Item> List { get { return (IEnumerable<Item>)GetValue(ListProperty); } set { SetValue(ListProperty, value); } }

    protected override void OnBindingContextChanged() { base.OnBindingContextChanged(); var itemList = BindingContext as List<Item>; if (itemList != null) List = itemList ; } }

    I want to bind a a whole List to the ListProperty.
    Why I am getting the Error:
    Binding: Entities.Item can not be converted to type 'System.Collections.ObjectModel.ObservableCollection``1[Entities.Item]

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    Most likely your issues here is that you are doing bad casting in your code and it cant' cast it as it says.

    Look at your code.... your BindableProperty is wrong:

     public static readonly BindableProperty ListProperty =
        BindableProperty.Create("List", typeof(IEnumerable<Sentence>), typeof(SentenceCell));
    

    Should be:

     public static readonly BindableProperty ListProperty =
        BindableProperty.Create("List", typeof(IEnumerable<Item>), typeof(ItemCell));
    
  • AdrnHAdrnH USMember ✭✭

    That was not the Problem, as I am replacing the Sentece with Item to make it more readable for you. I just forgot it there :S. The Problem really was to bind a List and how to organize this list'sdata within the linq querry in the viewmodel.

Sign In or Register to comment.