MSDN > Home page del forum > Windows Presentation Foundation (WPF) > I need a simple example of Grouping List View Items
Formula una domandaFormula una domanda
 

Con rispostaI need a simple example of Grouping List View Items

  • sabato 4 luglio 2009 17.54GameboyHippo Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    I'm very good at making winforms applicaitons.  I decided to start learning WPF so that I could run my applications in a web page or as a stand alone app.  The problem is that I'm having a lot of trouble getting up to speed.  I'm trying to make a user control that displays a particular object in groups.  Kind of like a winform ListView.  Everything was going great until I tried to group items.  Now I'm stuck.  Here's my XAML

    <UserControl x:Class="CarList"
    
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
        <StackPanel>
    
            <ListView Name="_uxCarList" MouseDoubleClick="_uxCarList_MouseDoubleClick">
    
                <CollectionViewSource Source="{Binding}">
    
                    <CollectionViewSource.GroupDescriptions>
    
                        <PropertyGroupDescription PropertyName="Make" />
    
                    </CollectionViewSource.GroupDescriptions>
    
                </CollectionViewSource>
    
                <ListView.View>
    
                    <GridView>
    
                        <GridViewColumn Header="Make" DisplayMemberBinding="{Binding Path=Make}"></GridViewColumn>
    
                        <GridViewColumn Header="Model" DisplayMemberBinding="{Binding Path=Model}"></GridViewColumn>
    
                        <GridViewColumn Header="Color" DisplayMemberBinding="{Binding Path=Color}"></GridViewColumn>
    
                    </GridView>
    
                </ListView.View>
    
            </ListView>
    
        </StackPanel>
    
    </UserControl>
    
    So inside my code behind I define a list of Cars (List<Cars>) and have a function that populates the listview.  The cars show up, but the groups don't. 

    I'm frustrated by how unintuitive WPF is.  Everything from the events to the XAML feels convoluted to me.  Where are the bare minimum examples? I don't want to know how to add groups from an XML document while doing the hokey pokey and making it into some expandable widget.  I want to know where examples are that say: Here's your object list.  Here's how to get them to show up in your list view with simple groups.  From there I can build up to all of the fancy stuff, but I'm desperate to learn the basics first.  Please help.
    • ModificatoGameboyHippo sabato 4 luglio 2009 17.55Took out random <br> tags that came from nowhere.
    •  

Risposte

  • sabato 4 luglio 2009 18.18Mariano O. Rodriguez Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    That is why you didn't define any template to show the groups, for example tou can add the default template that just show the text of the group:

    <ListView.GroupStyle>
        <x:Static Member="GroupStyle.Default"/>
    </ListView.GroupStyle>
    
    The CollectionViewSource should be defined as a resource:

    <UserControl.Resources>
        <CollectionViewSource x:Key="MyList" Source="{Binding}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Make" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    
    

    And finally you should bind the list with the CollectionViewSource:

    <ListView Name="_uxCarList" MouseDoubleClick="_uxCarList_MouseDoubleClick" ItemsSource="{Binding Source={StaticResource MyList}}">
    

    You have a complete example of grouping in this blog http://bea.stollnitz.com/blog/?p=19



    http://weblogs.asp.net/marianor/

Tutte le risposte

  • sabato 4 luglio 2009 18.18Mariano O. Rodriguez Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    That is why you didn't define any template to show the groups, for example tou can add the default template that just show the text of the group:

    <ListView.GroupStyle>
        <x:Static Member="GroupStyle.Default"/>
    </ListView.GroupStyle>
    
    The CollectionViewSource should be defined as a resource:

    <UserControl.Resources>
        <CollectionViewSource x:Key="MyList" Source="{Binding}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Make" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    
    

    And finally you should bind the list with the CollectionViewSource:

    <ListView Name="_uxCarList" MouseDoubleClick="_uxCarList_MouseDoubleClick" ItemsSource="{Binding Source={StaticResource MyList}}">
    

    You have a complete example of grouping in this blog http://bea.stollnitz.com/blog/?p=19



    http://weblogs.asp.net/marianor/