如何在 CollectionViewSource 中绑定项目列表? [英] How to bind a list of item in CollectionViewSource?

查看:17
本文介绍了如何在 CollectionViewSource 中绑定项目列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被这个问题困扰了好几天.一直没找到解决办法.所以我有一个名为Country 的项目列表,在这个列表中我还有另一个名为League 的项目列表.我已经创建了一个 CollectionViewSource 来在国家名称和联盟名称中组织所有这些,但我无法绑定所有联盟名称:

<CollectionViewSource.GroupDescriptions><PropertyGroupDescription PropertyName="名称"/><PropertyGroupDescription PropertyName="League.Name"/></CollectionViewSource.GroupDescriptions></CollectionViewSource>

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

但这只会绑定联赛列表的第一项.有什么解决办法吗?

实践示例

假设有两个国家(意大利、英格兰),意大利国家有两个联赛(意甲和意乙),英格兰只有(英超).意甲和意乙共有 3 场比赛,其中 2 场在意甲,1 场在意乙.英超联赛有 4 场比赛.在 ListView 组织中作为我的另一个主题

I'm stuck in this problem for days. Never found a solution. So I've a list of items called Country, in this list I've also another list of items called League. I've create a CollectionViewSource for organize all of this in nation name and league name but I can't bind all the leagues name:

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

I should write: <PropertyGroupDescription PropertyName="League[0].Name" />

but this only will bind the first item of the league list. Any solution?

PRACTICE EXAMPLE

suppose that there are two countries (Italy, England), Italy country have two leagues (Serie A and Serie B) and England have (Premiere League) only. Serie A and Serie B contains in total 3 matches, 2 in Serie A and 1 in Serie B. In premiere League there are 4 matches. In the ListView organization as my other topic that you can see here, in the UI should be displayed this structure:

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

Now Italy and england are disposed into groupstyle as serie a, serie b and premiere league, with an expander (as you can see in my other question) there is the matches for each leagues. The problem's that in the CollectionViewSource I cannot display the name of the league 'cause the leagues are inside a list, the same problem is for the matches, that is a list available in league, data structure:

Nations->Leagues->Matches

解决方案

I went by the output you want. Please check and tell if this is what you want.

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>

You can replace any of the ItemsControl with ListBox too.

Another solution using GroupStyle and ListView according to user requirements.

<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>

这篇关于如何在 CollectionViewSource 中绑定项目列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆