C# 如何在GroupedListView中实现AccordeonControl?(Xamarin.表格)
我对Xamarin.Forms和WPF基本上是新手。所以目前还没有使用ViewModels,因为我最近刚刚阅读了有关MVVM的内容,稍后将对其进行更改。我希望现在这不会有太大的不同 我们希望在分组的ListView()中实现一个AccordeonControl。 当点击比赛项目时,比赛应该展开 我实现了GroupedListView(没有accordeon),如下所示: ListView.xamlC# 如何在GroupedListView中实现AccordeonControl?(Xamarin.表格),c#,wpf,mvvm,xamarin,xamarin.forms,C#,Wpf,Mvvm,Xamarin,Xamarin.forms,我对Xamarin.Forms和WPF基本上是新手。所以目前还没有使用ViewModels,因为我最近刚刚阅读了有关MVVM的内容,稍后将对其进行更改。我希望现在这不会有太大的不同 我们希望在分组的ListView()中实现一个AccordeonControl。 当点击比赛项目时,比赛应该展开 我实现了GroupedListView(没有accordeon),如下所示: ListView.xaml 32 分组的ListView显然需要一个包含键并继承自ObservableCollection
32
分组的ListView显然需要一个包含键并继承自ObservableCollection的类,因此我创建了一个泛型类:
分组.cs
类分组:ObservableCollection
{
公钥{get;set;}
公共分组(K键,IEnumerable项)
{
钥匙=钥匙;
foreach(项目中的var项目)
此.Items.Add(item);
}
}
在代码隐藏中,我使用LINQ生成IEnumerable:
##
私有void UpdateListView(IEnumerable事件)
{
Items.Clear()
//按日期对事件排序,按日期分组。结果是分组类型的IEnumerable。
var sorted=来自事件中的raceEvent
orderby raceEvent.DateTime降序
按raceEvent.DateTime将raceEvent分组到grp中
选择新分组(grp.Key,grp);
foreach(已排序的变量项)
项目。添加(项目);
}
如何扩展此代码
提前谢谢
Niklas RammerstorferXamarin表单不包括手风琴控件,您必须自己编写。WPF和Xamarin表单是完全独立的技术,它们恰好都支持XAML。其他信息实际上只是我的问题o.o.的总结。我知道我必须实现我自己的,但我不知道如何做到这一点。这就是我问题的全部原因。试试这个:
<ListView>
<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>
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);
}
}
// 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);
}