Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How do I implement an AccordeonControl inside GroupedListView?

$
0
0

I am fairly new to Xamarin.Forms and WPF in general. So currently there are no ViewModels in use, because I have just recently read about MVVM and will change it later on. I hope this doesn't make much difference for now.

We want to implement an AccordeonControl inside a grouped ListView (Sketch is attached).
When clicking on a RaceEvent the races should be expanded.

I have implemented the GroupedListView (without accordeon) like follows:

ListView.xaml
`

  <ListView.GroupHeaderTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.Height>
          <OnPlatform x:TypeArguments="x:Double">
            <OnPlatform.iOS>
              32
            </OnPlatform.iOS>
          </OnPlatform>
        </ViewCell.Height>

        <Label Text="{Binding Path=Key, StringFormat='{0:dd.MM.yyyy}'}"
               VerticalOptions="CenterAndExpand"
               YAlign="Center"
               XAlign="Center"
               FontSize="32"/>
      </ViewCell>
    </DataTemplate>
  </ListView.GroupHeaderTemplate>

  <ListView.ItemTemplate>
    <DataTemplate>
      <ViewCell>
        <ViewCell.View>
          <ContentView Padding="10,0,0,0">
            <Label Text="{Binding Name}"
                   VerticalOptions="CenterAndExpand"
                   YAlign="Center"
                   FontSize="28"/>
           </ContentView>
        </ViewCell.View>
      </ViewCell>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>   

`

Grouped ListViews apparently require a class which contains a Key and inherits from ObservableCollection, so I made a generic class
Grouping
` class Grouping<K, T> : ObservableCollection
{
public K Key { get; set; }

    public Grouping(K key, IEnumerable<T> items)
    {
        Key = key;
        foreach (var item in items)
            this.Items.Add(item);
    }
}`

In the code behind I generate an IEnumerable<Grouping> using LINQ.
` private void UpdateListView(IEnumerable events)
{
Items.Clear();

        // Order events by dates and group by dates. The result is an IEnumerable of type Grouping.
        var sorted = from raceEvent in events
                     orderby raceEvent.DateTime descending
                     group raceEvent by raceEvent.DateTime into grp
                     select new Grouping<DateTime, IRaceEvent>(grp.Key, grp);

        foreach (var item in sorted)
            Items.Add(item);
    }`

How can I extend this code?

Thanks in advance

Niklas Rammerstorfer


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>