Microsoft Developer Network > 포럼 홈 > Windows Presentation Foundation (WPF) > I need a simple example of Grouping List View Items
질문하기질문하기
 

답변됨I need a simple example of Grouping List View Items

  • 2009년 7월 4일 토요일 오후 5:54GameboyHippo 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.
    • 편집됨GameboyHippo 2009년 7월 4일 토요일 오후 5:55Took out random <br> tags that came from nowhere.
    •  

답변

  • 2009년 7월 4일 토요일 오후 6:18Mariano O. Rodriguez 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨코드 있음
    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/

모든 응답

  • 2009년 7월 4일 토요일 오후 6:18Mariano O. Rodriguez 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨코드 있음
    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/