我正在構建向用戶顯示匹配系列的實時結果的應用程序。我設置數據的結構如下: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