2016-07-24 57 views
0

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

但是這當然會顯示只有特定的物品..不是物品清單。我需要幫助來解決這種情況,我無法弄清楚。謝謝。

+0

我能理解你的DataSource國家>聯賽>活動。現在你試圖展示什麼?你能用一些符號來解釋嗎?或者有一個粗略的想法? – AnjumSKhan

+0

@AnjumSKhan請在這裏查看練習示例:http://stackoverflow.com/questions/38551511/how-to-bind-a-list-of-item-in-collectionviewsource是相關的問題,在這個問題還有其他與其他主題相關的詳細信息,謝謝 – Heisenberg

回答

1

如果你想使用ListView的分組能力,你必須提供一個你想分組的物品(在你的情況下,聯賽)的平面列表,而不是標題項。 CollectionView通過指定GroupDescriptions來完成分組。

例如,假設League類有一個Country屬性:

class ViewModel 
{ 
    public ObservableCollection<Models.Country> Country { get; } 

    public IEnumerable<League> AllLeagues => Country.SelectMany(c => c.Leagues); 
} 

public class League 
{ 
    public string Name { get; set; } 
    public List<Event> Event { get; set; } 
    // add Country here 
    public Country Country { get; set; } 
} 

class 

<CollectionViewSource Source="{Binding AllLeagues}" x:Key="GroupedItems"> 
    <CollectionViewSource.GroupDescriptions> 
     <PropertyGroupDescription PropertyName="Country" /> 
    </CollectionViewSource.GroupDescriptions> 

然後當你綁定的列,則直接綁定到League特性,例如:

<GridViewColumn Header="Date" DisplayMemberBinding="{Binding Path=Event.MatchDate}"/> 

而在組風格中,您可以像以前那樣綁定到Country屬性。

替代解決方案

如果你想顯示在WPF任何分層數據您可以使用爲它建造(如Xceed數據網格)的控制或共同破解它內置的WPF數據網格的行細節。

這裏是一個示例XAML爲此(注意它使用您的原始數據結構沒有我上面建議的修改)。這些基本上是3個嵌套在一起的數據網格。每個網格都有自己的一組列,所以你可以爲每個級別定義任何你想要的(國家,聯盟,事件)。

<Window x:Class="WpfApp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     xmlns:app="clr-namespace:WpfApp" 
     d:DataContext="{d:DesignData ViewModel}"> 
    <FrameworkElement.Resources> 
     <app:VisibilityToBooleanConverter x:Key="VisibilityToBooleanConverter" /> 
     <DataTemplate x:Key="HeaderTemplate"> 
      <Expander IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGridRow}, Path=DetailsVisibility, Converter={StaticResource VisibilityToBooleanConverter}}" /> 
     </DataTemplate> 
     <Style x:Key="DataGridStyle" 
       TargetType="DataGrid"> 
      <Setter Property="RowHeaderTemplate" 
        Value="{StaticResource HeaderTemplate}" /> 
      <Setter Property="RowDetailsVisibilityMode" 
        Value="Collapsed" /> 
      <Setter Property="AutoGenerateColumns" 
        Value="False" /> 
      <Setter Property="IsReadOnly" 
        Value="True" /> 
     </Style> 
    </FrameworkElement.Resources> 
    <Grid> 
     <DataGrid ItemsSource="{Binding Country}" 
        Style="{StaticResource DataGridStyle}"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Header="Name" 
            Binding="{Binding Name}" /> 
      </DataGrid.Columns> 
      <DataGrid.RowDetailsTemplate> 
       <DataTemplate> 
        <DataGrid ItemsSource="{Binding League}" 
           Style="{StaticResource DataGridStyle}"> 
         <DataGrid.Columns> 
          <DataGridTextColumn Header="Name" 
               Binding="{Binding Name}" /> 
         </DataGrid.Columns> 
         <DataGrid.RowDetailsTemplate> 
          <DataTemplate> 
           <DataGrid ItemsSource="{Binding Event}" 
              AutoGenerateColumns="False" 
              IsReadOnly="True"> 
            <DataGrid.Columns> 
             <DataGridTextColumn Header="Match Home" 
                  Binding="{Binding MatchHome}" /> 
            </DataGrid.Columns> 
           </DataGrid> 
          </DataTemplate> 
         </DataGrid.RowDetailsTemplate> 
        </DataGrid> 
       </DataTemplate> 
      </DataGrid.RowDetailsTemplate> 
     </DataGrid> 
    </Grid> 
</Window> 

您還需要爲我所用的轉換器的代碼:

public class VisibilityToBooleanConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     => value as Visibility? == Visibility.Visible; 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     => value as bool? == true ? Visibility.Visible : Visibility.Collapsed; 
} 
+0

或多或少,你已經集中了這一點,但我有一個可觀察的國家集合,其中包含包含比賽的聯賽。真正的問題在於,我無法在GridViewColumn標題和GroupStyle(擴展器)中顯示聯盟的名稱和每個比賽的細節,原因都在列表中。 – Heisenberg

+0

如果您想使用「CollectionView」的本地分組支持,您將*具有*對數據建模有點不同。您可以創建一箇中間層模型,以我描述的方式排列數據。 –

+0

我明白你的意思,但我需要設置哪種結構?因爲我需要具有國家 - >聯賽 - >比賽的組織結構 – Heisenberg