Xaml 如何将两个不同列表中的两个对象绑定到一个 ListView 控制器中?
How do I bind two objects from two different lists into one ListView controller in Xaml?
所以我有两个列表,一个是作者的名字,另一个是引用。
我需要两者都进入同一个 ListView 控制器。名称和引用来自两个不同的列表,必须是这样。
<ListView x:Name ="CategoryListView" ItemsSource ="{Binding Authors, Binding Quotes}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell >
<ViewCell.View >
<StackLayout>
<StackLayout Orientation="Horizontal" Padding="10">
<Label Text ="{Binding AuthorName}" FontSize="10" TextColor="White"/>
<Label Text="{Binding QuoteContent}" FontSize="10" TextColor="White"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是我试过的方法,但没有用。
这是我的代码:
public QuotePageViewModel(string category, IEnumerable<Quote> qoutes, List<Author> matchingAuthors)
{
Category = category;
Quotes = new List<Quote>();
Quotes = qoutes.ToList();
Authors = new List<Author>();
Authors = matchingAuthors.ToList();
}
这是我的建议:
您将列表绑定到字典。
我们会将您的标签绑定到字典的 Key 和 Value,在您的 Xaml 中只需这样做:
<ListView x:Name ="CategoryListView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell >
<ViewCell.View >
<StackLayout>
<StackLayout Orientation="Horizontal" Padding="10">
<Label Text ="{Binding Key}" FontSize="10" TextColor="White"/>
<Label Text="{Binding Value}" FontSize="10" TextColor="White"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
现在用 Authors 和 Quotes 填充字典(这是假设 quotes 和 churchfathers 的长度相同并且索引匹配,所以 Quote[0] 与 Authors[0] 匹配)。并将其添加为列表的 ItemsSources:
var dict = new Dictionary<string, string>();
for(int i = 0; i<quotes.Count; i++)
{
dict.Add(Authors[i], quotes.ElemetAt(i));
}
CategoryListView.ItemsSource = dict;
这应该适合您,但请记住,Dictionary 没有实现 INotifyPropertyChanged,因此不会显示运行时内容的更改
所以我有两个列表,一个是作者的名字,另一个是引用。 我需要两者都进入同一个 ListView 控制器。名称和引用来自两个不同的列表,必须是这样。
<ListView x:Name ="CategoryListView" ItemsSource ="{Binding Authors, Binding Quotes}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell >
<ViewCell.View >
<StackLayout>
<StackLayout Orientation="Horizontal" Padding="10">
<Label Text ="{Binding AuthorName}" FontSize="10" TextColor="White"/>
<Label Text="{Binding QuoteContent}" FontSize="10" TextColor="White"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是我试过的方法,但没有用。 这是我的代码:
public QuotePageViewModel(string category, IEnumerable<Quote> qoutes, List<Author> matchingAuthors)
{
Category = category;
Quotes = new List<Quote>();
Quotes = qoutes.ToList();
Authors = new List<Author>();
Authors = matchingAuthors.ToList();
}
这是我的建议: 您将列表绑定到字典。 我们会将您的标签绑定到字典的 Key 和 Value,在您的 Xaml 中只需这样做:
<ListView x:Name ="CategoryListView" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate >
<ViewCell >
<ViewCell.View >
<StackLayout>
<StackLayout Orientation="Horizontal" Padding="10">
<Label Text ="{Binding Key}" FontSize="10" TextColor="White"/>
<Label Text="{Binding Value}" FontSize="10" TextColor="White"/>
</StackLayout>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
现在用 Authors 和 Quotes 填充字典(这是假设 quotes 和 churchfathers 的长度相同并且索引匹配,所以 Quote[0] 与 Authors[0] 匹配)。并将其添加为列表的 ItemsSources:
var dict = new Dictionary<string, string>();
for(int i = 0; i<quotes.Count; i++)
{
dict.Add(Authors[i], quotes.ElemetAt(i));
}
CategoryListView.ItemsSource = dict;
这应该适合您,但请记住,Dictionary 没有实现 INotifyPropertyChanged,因此不会显示运行时内容的更改