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
I have implemented now something that works for me. A bit different datastructure from that what James has propesed in his original post. If u have better ideas let me know.
var sorted =itemList. OrderBy(s => s.position). GroupBy(t => t.itemTitle). Select(grp => { return new Grouping<string, Item>(grp.Key, grp); } );
`itemListSorted = new CustomObservableCollection<Grouping<string, List>>();
foreach (var group in sorted) { var list = new List<List<Item>>(); var l = new List<Item>(); foreach (var item in group) { l.Add(item); } list.Add(l); var grouped = new Grouping<string, List<Item>>(group.Key, list); itemListSorted.Add(grouped); }`
Answers
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);
YOu would need something like:
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]
I have implemented now something that works for me. A bit different datastructure from that what James has propesed in his original post. If u have better ideas let me know.
var sorted =itemList. OrderBy(s => s.position). GroupBy(t => t.itemTitle). Select(grp => { return new Grouping<string, Item>(grp.Key, grp); } );
`itemListSorted = new CustomObservableCollection<Grouping<string, List>>();
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:
Should be:
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.