Results 1 to 2 of 2

Thread: Cannot bind list of items in GridView column

  1. #1

    Thread Starter
    Addicted Member JackIlPazzo's Avatar
    Join Date
    Oct 2014

    Cannot bind list of items in GridView column

    I'm building an application that show to user the live result of a matches series. I setup the structure of data as follows: `Countries->Leagues->Matches`
    In particular in the ViewModel I've created an observable collection of countries as follows:

      private ObservableCollection<Models.Country> _countries = new ObservableCollection<Models.Country>();
        public ObservableCollection<Models.Country> Country
            get { return _countries; }
    and the model:

    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; }
    the class Event contains the properties of each event, in particular the name of the event, the date and so on..

    I valorize this data as follows:

    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>();
        country.League = new List<League>();
        lsVm.Country.Add(country); //lsVm contains the ViewModel
    How you can see I create an object called `country` (Italy) that will contains in this case two leagues (Serie A) and (Serie B). Each league contains one match actually in playing `Serie A -> Inter` and `Serie B -> Milan`

    I add the league two the country, and finally the country to the observable collection in the viewmodel. Until here no problem. The problem's come in the xaml.

    So I've organized all of this stuff inside of GroupViews, for doing this I'm using a CollectionViewSource, in particular:

    <CollectionViewSource Source="{Binding Country}" x:Key="GroupedItems">
                    <PropertyGroupDescription PropertyName="Name" />
                    <PropertyGroupDescription PropertyName="League.Name" />
    the code above is located in my Window.Resources, and tell to CollectionViewSource to organize for country name and leagues name the respective leagues associated.
    I've two ListView as this:

    <ListView ItemsSource="{Binding Source={StaticResource GroupedItems}}" Name="Playing">
                                    <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}"/>
                                        <Style TargetType="{x:Type GroupItem}">
                                            <Setter Property="Template">
                                                        <Expander IsExpanded="True" Background="#4F4F4F" >
                                                                <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" />
                                                            <ItemsPresenter />
    The GroupStyle contains the leagues that will contains each match, now the problem is that I can't see any league and any match 'cause this item are inside of a list. So for display them I should write in the xaml this code:

    <PropertyGroupDescription PropertyName="League[0].Name" />
    this fix the bug of the league name displayed into GroupStyle
    and in the GridView:

    <GridViewColumn Header="Casa" Width="150" DisplayMemberBinding="{Binding Path = League[0].Event[0].MatchHome}"/>
    but this of course will display only the specific item.. not the list of items. I need help to fix this situation, I cannot figure out. Thanks.

  2. #2

    Re: Cannot bind list of items in GridView column

    If you want to use the ListView's grouping abilities, you have to provide it a flat list of the items you want to group (in your case, the leagues), not the header items. The CollectionView does the grouping for you by specifying GroupDescriptions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width