none
Expander as ListView Item RRS feed

  • 问题

  • I need to do a listview that has a muti-column . where the row is an expander.In particular.I have datacollection binding view,I want to show another gridview In expander,expander as Listview Item in listview.so have any case,Thank you for you help




    2014年1月7日 8:13

答案

  • 你好,

    对于你的需求,我们可以在ListView中实现一个GridView,并且定制ListViewItem的模版,以下是我的实例:

    <Window.Resources>
            <!--xml data-->
            <XmlDataProvider x:Key="MyData" XPath="/Info/Item">
                <x:XData>
                    <Info xmlns="">
                        <Item ID="12345" Name="Book 1" Price="$32.05"
                    Author="Author A" Catalog="Business"/>
                        <Item ID="13590" Name="Book 2" Price="$10.00"
                    Author="Author B" Catalog="Language"/>
                        <Item ID="24678" Name="Book 3" Price="$9.00"
                    Author="Author C" Catalog="Language"/>
                        <Item ID="65432" Name="Book 4" Price="$8.50"
                    Author="Author D" Catalog="Business"/>
                        <Item ID="11233" Name="Book 5" Price="$19.00"
                    Author="Author E" Catalog="Health"/>
                        <Item ID="94837" Name="Book 6" Price="$8.50"
                    Author="Author F" Catalog="Language"/>
                    </Info>
                </x:XData>
            </XmlDataProvider>
            <!--CollectionViewSource-->
            <CollectionViewSource x:Key='src' Source="{Binding Source={StaticResource MyData}}" />
        </Window.Resources>
        <!--Content-->
        <ListView ItemsSource='{Binding Source={StaticResource src}}'
                BorderThickness="0">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="Margin" Value="0,0,0,5"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListViewItem}">
                                <Expander IsExpanded="True"
                                                  BorderBrush="#FFA4B97F"
                                                  BorderThickness="0,0,0,1">
                                    <Expander.Header>
                                        <DockPanel>
                                            <TextBlock FontWeight="Bold" Text="Expander"
                                                               Margin="5,0,0,0" Width="100"/>
                                        </DockPanel>
                                    </Expander.Header>
                                    <Expander.Content>
                                        <GridViewRowPresenter Content="{TemplateBinding Content}"
                                                              Columns="{TemplateBinding GridView.ColumnCollection}"/>
                                    </Expander.Content>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID"
                            DisplayMemberBinding="{Binding XPath=@ID}"
                            Width="100" />
                    <GridViewColumn Header="Name"
                            DisplayMemberBinding="{Binding XPath=@Name}"
                            Width="140" /> 
                    <GridViewColumn Header="Price"
                            DisplayMemberBinding="{Binding XPath=@Price}"
                            Width="80" />
                    <GridViewColumn Header="Author"
                            DisplayMemberBinding="{Binding XPath=@Author}"
                            Width="80" />
                </GridView>
            </ListView.View>
        </ListView>

    效果图:


    以下的文档可以给你一些有用的帮助:

    #How to: Style a Row in a ListView That Implements a GridView
    http://msdn.microsoft.com/en-us/library/ms743659(v=vs.110).aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月8日 8:25
    版主