[英]C# Xamarin Forms Listview set BindingContext to nested List
I have a classic ListView
in a Xamarin Form
and I am trying to bind it's content.我在
Xamarin Form
有一个经典的ListView
,我正在尝试绑定它的内容。 I come from UWP where we use CollectionViewSource
and here things are a bit different.我来自我们使用
CollectionViewSource
UWP,这里的情况有点不同。
XAML XAML
<StackLayout>
<ListView x:Name="listView"
ItemsSource="{Binding Monkeys}"
Header="{Binding Intro}"
Footer="{Binding Summary}">
<ListView.HeaderTemplate >
<DataTemplate>
<StackLayout Orientation="Horizontal"
Padding="10,5,5,10"
BackgroundColor="Yellow">
<Label Text="~~"/>
<Label Text="{Binding .}"/>
<Label Text="~~"/>
</StackLayout>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Description}"></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
Background code后台代码
var results = new List<Matches> {
new Matches {Name="Xander", Description="Writer"},
new Matches {Name="Rupert", Description="Engineer"},
new Matches {Name="Tammy", Description="Designer"},
new Matches {Name="Blue", Description="Speaker"},
};
var header = new List<Titles> { new Titles { Monkeys = results, Intro = "This is a big header", Summary = "This is the end" } };
header.Add(new Titles { Monkeys = results, Intro = "Shift", Summary = "Yeah" } );
//BindingContext = header;
BindingContext = new Titles { Monkeys = results, Intro = "This is a big header", Summary = "This is the end"};
class Matches
{
public string Name { get; set; }
public string Description { get; set; }
}
class Titles
{
public List<Matches> Monkeys { get; set; }
public string Intro { get; set; }
public string Summary { get; set; }
}
As you see on the line commented out, it doesn't bind the List, hence commented out :) Any idea if it is possible to bind it's content with nested list with multiple headers and items?正如您在注释掉的行上看到的那样,它不绑定列表,因此被注释掉了 :) 知道是否可以将其内容与具有多个标题和项目的嵌套列表绑定? What am I doing wrong here?
我在这里做错了什么?
According to your data, I think you shoud to display a group data list.根据你的数据,我认为你应该显示一个组数据列表。 You can try the following code:
您可以尝试以下代码:
class Matches班级比赛
public class Matches
{
public string Name { get; set; }
public string Description { get; set; }
}
class Titles班级名称
public class Titles : ObservableCollection<Matches>
{
//public List<Matches> Monkeys { get; set; }
public string Intro { get; set; }
public string Summary { get; set; }
public Titles(List<Matches> list):base(list)
{
}
}
class MainPage类 MainPage
public partial class MainPage : ContentPage
{
public ObservableCollection<Titles> sortedItems { get; set; }
public ObservableCollection<Matches> items { get; set; }
public MainPage()
{
InitializeComponent();
listView.ItemsSource = getData();
}
public List<Titles> getData() {
items = new ObservableCollection<Matches> {
new Matches {Name="Xander", Description="Writer"},
new Matches {Name="Rupert", Description="Engineer"},
new Matches {Name="Tammy", Description="Designer"},
new Matches {Name="Blue", Description="Speaker"},
};
List<Titles> sortedItems = new List<Titles>() {
new Titles(items.ToList()){ Intro = "This is a big header", Summary = "This is the end" },
new Titles(items.ToList()){ Intro = "Shift", Summary = "Yeah" },
new Titles(items.ToList()){ Intro = "This is a big header", Summary = "This is the end" },
};
return sortedItems;
}
}
MainPage.xaml主页.xaml
<StackLayout>
<ListView
HasUnevenRows="True"
x:Name="listView"
IsGroupingEnabled = "True">
<ListView.GroupHeaderTemplate >
<DataTemplate>
<ViewCell Height="40">
<StackLayout Orientation="Horizontal"
BackgroundColor="#3498DB"
VerticalOptions="FillAndExpand">
<Label Text="{Binding Intro}"
VerticalOptions="Center" />
<Label Text=" "/>
<Label Text="{Binding Summary}"
VerticalOptions="Center" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" Detail="{Binding Description}"></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
The effect is as follows:效果如下:
Note:笔记:
According to my understanding, you want to display these two fields in the position of each group List, right?按照我的理解,你是想在每个组List的位置显示这两个字段,对吗? Then
ListView.GroupHeaderTemplate
will meet our need.And you can custom the layout of ListView.GroupHeaderTemplate
as you want,so you can show the Intro
and Summary
at the same time.那么
ListView.GroupHeaderTemplate
就可以满足我们的需要了。你可以根据需要自定义ListView.GroupHeaderTemplate
的布局,这样你就可以同时显示Intro
和Summary
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.