2016-07-24 17 views
0

我被困在这个问题上好几天了。从未找到解决方案。所以我有一个名为Country的项目列表,在这个列表中我还有另一个名为League的项目列表。我已经创建一个CollectionViewSource的组织在国名和联盟名称这一切,但我不能约束所有的联赛名称:如何绑定CollectionViewSource中的项目列表?

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

我应该写:<PropertyGroupDescription PropertyName="League[0].Name" />

但这只会绑定联赛名单中的第一项。任何解决方案

实践范例

假设有两个国家(意大利,英国),意大利国家只有两个联赛(意甲和意乙)和英格兰有(超级联赛)。 意甲联赛和乙级联赛共包含3场比赛,2场意甲联赛和1场意甲联赛。在首映联赛中共有4场比赛。 在ListView组织作为我的其他话题,you can see here,在UI应显示这种结构:

ITALY 
    SERIE A 
      INTER 
      MILAN 
    SERIE B 
      JUVENTUS 
ENGLAND 
    PREMIERE LEAGUE 
      LEICESTER 
      MANCHESTER UNITED 
      MANCHESTER CITY 
      LIVERPOOL 

现在意大利和英格兰被布置成groupstyle作为意甲,意乙和超级联赛,与膨胀(正如你可以在我的另一个问题中看到的那样)每个联赛都有比赛。 的问题是,在CollectionViewSource我无法显示联盟中引起了联盟的名单中,同样的问题是比赛的名字,那是联盟提供的列表,数据结构:

Nations->Leagues->Matches 
+0

(1)目前尚不清楚你的意图是什么。如果'League'是一个集合,'League.Name'应该返回什么? (2)'CollectionViewSource'不像'Binding'那样使用属性路径,所以你只能指定一个属性名称。 (3)你应该显示你对这个集合的约束力。它是两个列表(主细节)?树视图? –

+0

发布更多的cs代码,请准确得到你想要的。 –

+0

@EliArbel我在这里发布了一个更完整的场景:http://stackoverflow.com/questions/38549818/cannot-bind-list-of-items-in-gridview-column不幸的是没有人回答 – Heisenberg

回答

1

我去了你想要的输出。请检查并确定这是否是您想要的。

public partial class WinExpander : Window 
{ 
    public WinExpander() 
    { 
     InitializeComponent(); 

     this.DataContext = new ViewModel(); 
    } 
} 

public class ViewModel 
    { 
     public List<Country> Countries { get; set; } 
     public ViewModel() 
     { 
      Countries = new List<Country>(); 
     } 
    } 

public class Country 
    { 
     public string Name { get; set; } 
     public List<League> Leagues { get; set; } 

     public Country() 
     { 
      Leagues = new List<League>(); 
     } 
    } 

    public class League 
    { 
     public string Name { get; set; } 
     public List<Match> Matches { get; set; } 

     public League() 
     { 
      Matches = new List<Match>(); 
     } 
    } 

    public class Match 
    { 
     public string Name { get; set; } 
    } 

XAML

<Window.Resources> 
    <CollectionViewSource x:Key="GroupedItems" Source="{Binding Countries}"/> 
</Window.Resources> 
... 
<ItemsControl ItemsSource="{Binding Source={StaticResource GroupedItems}}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Expander Header="{Binding Name}"> 
         <ItemsControl ItemsSource="{Binding Leagues}" Margin="25 0 0 0"> 
          <ItemsControl.ItemTemplate> 
           <DataTemplate> 
            <Expander Header="{Binding Name}"> 
             <ItemsControl ItemsSource="{Binding Matches}" Margin="25 0 0 0"> 
              <ItemsControl.ItemTemplate> 
               <DataTemplate> 
                <TextBlock Text="{Binding Name}"/> 
               </DataTemplate> 
              </ItemsControl.ItemTemplate> 
             </ItemsControl> 
            </Expander> 
           </DataTemplate> 
          </ItemsControl.ItemTemplate> 
         </ItemsControl> 
        </Expander>   
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
</ItemsControl> 

您可以ListBox替换任何ItemsControl太多。

根据用户要求使用GroupStyleListView的另一种解决方案。

<CollectionViewSource x:Key="GroupedItems" Source="{Binding Countries}"> 
    <CollectionViewSource.GroupDescriptions> 
     <PropertyGroupDescription PropertyName="Name"/> 
    </CollectionViewSource.GroupDescriptions> 
</CollectionViewSource> 
... 
<ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}" Name="Playing"> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Country" Width="150" DisplayMemberBinding="{Binding Source={x:Static sys:String.Empty}}" /> 
      <GridViewColumn Header="Leagues">       
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <ItemsControl ItemsSource="{Binding Leagues}" Margin="25 0 0 0"> 
          <ItemsControl.ItemTemplate> 
           <DataTemplate> 
            <Grid HorizontalAlignment="Stretch" Background="#FFBCDAEC"> 
             <TextBlock FontSize="18" Padding="5" Text="{Binding Name}"/> 
            </Grid> 
           </DataTemplate> 
          </ItemsControl.ItemTemplate> 
         </ItemsControl> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
    <ListView.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.ContainerStyle> 
       <Style TargetType="{x:Type GroupItem}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <Expander IsExpanded="True"> 
            <Expander.Header> 
             <TextBlock Text="{Binding Name}" Foreground="Red" FontSize="22" VerticalAlignment="Bottom" /> 
            </Expander.Header> 
            <ItemsPresenter /> 
           </Expander> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </GroupStyle.ContainerStyle> 
     </GroupStyle> 
    </ListView.GroupStyle> 
</ListView> 

enter image description here

+0

非常好的答案,我测试你的代码,并试图转换为GroupStyle,这可能吗?检查我的组风格结构在这里寻求帮助:http://stackoverflow.com/questions/38549818/cannot-bind-list-of-items-in-gridview-column – Heisenberg

+0

@海森堡我不明白为什么你想在这里使用分组。分组是另一回事。我仍然会尝试转换为GroupStyle。 – AnjumSKhan

+0

groupstyle为我显示了一个更好的结构,我会尝试将您的代码转换为groupstyle。我希望你的帮助,谢谢:) – Heisenberg

相关问题