使用Listview项和标题绑定字典
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Listview项和标题绑定字典相关的知识,希望对你有一定的参考价值。
我正在尝试将Dictionary>与列表视图绑定。将标题设置为Idictionary的键,将约会对象列表设置为项目。但数据没有受到约束。
字符串(键)需要与标题元素绑定,内部列表列表包含该标题下的对象。
我怎样才能解决这个问题?
<ListView ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollMode="Auto" ScrollViewer.HorizontalScrollMode="Auto"
ItemsSource="{Binding Source={StaticResource EventsObj}}">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<Textblock Text="{Binding EventsObj.Value.UserName}"/>
<Textblock Text="{Binding EventsObj.Value.VisitorFor}"/>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.HeaderTemplate>
<DataTemplate>
<Grid Margin="5,10,0,10" Background="Black">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="68*"/>
<ColumnDefinition Width="21*"/>
</Grid.ColumnDefinitions>
<TextBlock Foreground="White" Text="{Binding EventsObj.Key}">
</TextBlock>
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
C#中的对象是
public class Appointment
{
public Dictionary<DateTime, List<Appointments>> Data {get; set;}
}
public class Appointments
{
public string UserName {get; set;}
public string VisitorFor {get; set;}
}
List<Appointments> Datas = new List<Appointments>();
Datas = new List<Aprio.Appointments>();
Datas.Add(new Appointments { UserName = "Pradeep", VisitorFor = "John Sam" });
Datas.Add(new Appointments { UserName = "Danny", VisitorFor = "John Sam" });
Datas.Add(new Appointments { UserName = "Ram", VisitorFor = "John Sam" });
Datas.Add(new Appointments { UserName = "Siva", VisitorFor = "John Sam" });
Datas.Add(new Appointments { UserName = "Ragu", VisitorFor = "John Sam" });
this.Appointment.Add("John", Datas);
List<Appointments> Datas2 = new List<Appointments>();
Datas2 = new List<Appointments>();
Datas2.Add(new Appointments { UserName = "Guru", VisitorFor = "Sam Hilferg" });
Datas2.Add(new Appointments { UserName = "Jack", VisitorFor = "Sam Hilferg" });
Datas2.Add(new Appointments { UserName = "Bob", VisitorFor = "Sam Hilferg" });
this.Appointment.Add("Sam", Datas2);
答案
更改ListView ItemTemplate。试试这个!
<ListView.ItemTemplate>
<DataTemplate>
<ListView DataContext="{Binding EventsObj.Value}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Textblock Text="{Binding UserName}"/>
<Textblock Text="{Binding VisitorFor}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</ListView.ItemTemplate>
另一答案
我把标题移到了右边。在MSDN找到了这个答案。它对我有用。
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid GroupHeaderPlacement="Left"/>
</ItemsPanelTemplate>
</GridView.ItemsPanel>
以上是关于使用Listview项和标题绑定字典的主要内容,如果未能解决你的问题,请参考以下文章
片段中 ListView 的 setOnItemClickListener