【问题标题】:How to bind a list of item in CollectionViewSource?如何绑定 CollectionViewSource 中的项目列表?
【发布时间】:2016-07-24 11:25:13
【问题描述】:

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

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

我应该写:&lt;PropertyGroupDescription PropertyName="League[0].Name" /&gt;

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

实践示例

假设有两个国家(意大利、英格兰),意大利国家有两个联赛(意甲和意乙),而英格兰只有(英超联赛)。 意甲和意乙共包含 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

现在意大利和英格兰被安排为意甲、意甲和英超联赛的小组赛,每个联赛都有一个扩展器(正如你在我的另一个问题中看到的那样)。 问题是在 CollectionViewSource 中我无法显示联赛的名称,因为联赛在列表中,同样的问题是比赛,这是联赛中可用的列表,数据结构:

Nations->Leagues->Matches

【问题讨论】:

  • (1) 不清楚你的意图是什么。如果League 是一个集合,League.Name 应该返回什么? (2) CollectionViewSource 不像Binding 那样使用属性路径,所以你只能指定一个属性名称。 (3) 你应该展示你绑定到这个集合的对象。是两个列表(主详细信息)吗?树视图?
  • 贴出更多的cs代码,请得到你想要的。
  • @EliArbel 我在这里发布了一个更完整的场景:stackoverflow.com/questions/38549818/… 不幸的是没有人回答
  • @HenkaProgrammer 我需要按国家名称和联赛名称组织组描述
  • 所以你的意思是对国家名称进行分组,在每个组中你想对联赛进行分组??

标签: c# wpf


【解决方案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>

您也可以将任何ItemsControl 替换为ListBox

根据用户要求使用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>

【讨论】:

  • 很好的答案,我正在测试您的代码并尝试转换为 GroupStyle,这可能吗?在此处查看我的组样式结构以获取帮助:stackoverflow.com/questions/38549818/…
  • @Heisenberg 我不明白你为什么要在这里使用分组。分组是另一回事。我仍然会尝试转换为 GroupStyle。
  • groupstyle 为我展示了更好的结构,我会尝试将您的代码转换为 groupstyle。我希望你的帮助,谢谢:)
  • @Heisenberg 在我们每周在印度的集市上购买蔬菜、水果等。现已上线。
  • @Heisenberg 嗨,我已经完成了你为联赛名称应用分组的工作。快来聊天吧!
猜你喜欢
  • 2011-04-05
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多