我正在构建向用户显示匹配系列的实时结果的应用程序。我设置数据的结构如下:Countries->Leagues->Matches
尤其是在视图模型我创建国家可观察到的集合,如下所示:无法绑定GridView列中的项目列表
private ObservableCollection<Models.Country> _countries = new ObservableCollection<Models.Country>();
public ObservableCollection<Models.Country> Country
{
get { return _countries; }
}
和模型:
public class Country
{
public string Name { get; set; }
public List<League> League { get; set; }
}
public class League
{
public string Name { get; set; }
public List<Event> Event { get; set; }
}
类事件包含每个事件的属性,特别是事件的名称,日期等等。
我按如下方式确定这个数据:
Country country = new Country();
country.Name = "Italy";
League league = new League();
league.Name = "Serie A";
League league2 = new League();
league2.Name = "Serie B";
Event @event = new Event();
@event.MatchHome = "Inter";
Event event2 = new Event();
@event.MatchHome = "Milan";
league.Event = new List<Event>();
league2.Event = new List<Event>();
league.Event.Add(@event);
league2.Event.Add(event2);
country.League = new List<League>();
country.League.Add(league);
country.League.Add(league2);
lsVm.Country.Add(country); //lsVm contains the ViewModel
你如何看到我创建了一个名为country
(意大利)的对象,在这种情况下将包含两个联赛(意甲)和(意乙)。每个联赛实际上包含一场比赛实际上在打Serie A -> Inter
和Serie B -> Milan
我添加了联赛两国的国家,最后是国家在视图模型的可观察收藏。直到这里没问题。问题出现在xaml中。
所以我组织的所有的这些东西GroupViews内,这样做我使用CollectionViewSource,特别是:
<CollectionViewSource Source="{Binding Country}" x:Key="GroupedItems">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Name" />
<PropertyGroupDescription PropertyName="League.Name" />
</CollectionViewSource.GroupDescriptions>
上面的代码位于我的窗口。资源,并告诉CollectionViewSource组织国家名称和联赛名称相关的各个联赛。 我有2个ListView的,因为这:
<ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}" Name="Playing">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="150" DisplayMemberBinding="{Binding Path = League.Event.MatchDate}"/>
<GridViewColumn Header="Minutes" Width="70" DisplayMemberBinding="{Binding Path = League.Event.MatchMinute}"/>
<GridViewColumn Header="Home" Width="150" DisplayMemberBinding="{Binding Path = League.Event.MatchHome}"/>
<GridViewColumn Header="Score" Width="100" DisplayMemberBinding="{Binding Path = League.Event.MatchScore}"/>
<GridViewColumn Header="Away" Width="150" DisplayMemberBinding="{Binding Path = League.Event.MatchAway}"/>
</GridView>
</ListView.View>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True" Background="#4F4F4F" >
<Expander.Header>
<StackPanel Orientation="Horizontal" Height="22">
<TextBlock Text="{Binding Name}" FontWeight="Bold" Foreground="White" FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22" Foreground="Orange" FontWeight="Bold" FontStyle="Italic" Margin="10,0,0,0" VerticalAlignment="Bottom" />
<TextBlock Text=" Leagues" FontSize="22" Foreground="White" FontStyle="Italic" VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
的GroupStyle包含将包含每个匹配的联赛,现在的问题是,我看不到任何联赛的任何匹配“造成这个项目是一个内名单。因此,对于显示他们,我应该在XAML编写代码:
<PropertyGroupDescription PropertyName="League[0].Name" />
此修复程序显示为GroupStyle 和GridView中联赛名称的错误:
<GridViewColumn Header="Casa" Width="150" DisplayMemberBinding="{Binding Path = League[0].Event[0].MatchHome}"/>
但是这当然会显示只有特定的物品..不是物品清单。我需要帮助来解决这种情况,我无法弄清楚。谢谢。
我能理解你的DataSource国家>联赛>活动。现在你试图展示什么?你能用一些符号来解释吗?或者有一个粗略的想法? – AnjumSKhan
@AnjumSKhan请在这里查看练习示例:http://stackoverflow.com/questions/38551511/how-to-bind-a-list-of-item-in-collectionviewsource是相关的问题,在这个问题还有其他与其他主题相关的详细信息,谢谢 – Heisenberg