Getting ListView to do things other than text

I'm having trouble customizing the ListView for my application's needs. What it needs to do is:

a) Allow multiple rows for each item bound, not just one.
b) Support things other than just text for binding (DisplayMemberPath).

As an example, I want a listview with four columns. Each ListViewItem is bound to a strongly-typed class and will require two rows:

For example, take a look at this mockup, which represents a ListViewItem. The lines are grid lines (it's easy to see the required two rows). I need a Date/Time field bound to the top left cell, a text field in the top middle left, and a decimal in both top rights. Finally, there needs to be text bound in the bottom middle/right.



Answer this question

Getting ListView to do things other than text

  • Taveira

    Almost.

    The second ListView you defined does what I want with the two rows per item. My question is, can I have two ListView column headers in the second ListView that resize the two Grid columns you defined in your data template


  • seve7

    Try it.

    <StackPanel>
    <StackPanel.Resources>

    <XmlDataProvider x:Key="MyData" XPath="/Info">
    <x:XData>
    <Info xmlns="">
    <Song Name="Song1" Time="3:54" Artist="Singer1" Level="3" Disk="Disk1" />
    <Song Name="Song2" Time="4:31" Artist="Singer2" Level="4" Disk="Disk3"/>
    <Song Name="Song3" Time="3:54" Artist="Singer3" Level="5" Disk="Disk1" />
    <Song Name="Song4" Time="3:54" Artist="Singer3" Level="4" Disk="Disk2" />
    <Song Name="Song5" Time="3:54" Artist="Singer1" Level="5" Disk="Disk3" />
    </Info>
    </x:XData>
    </XmlDataProvider>

    <GridViewColumnCollection x:Key="c1">
    <GridViewColumn DisplayMemberPath="@Name" Header="Name"/>
    <GridViewColumn DisplayMemberPath="@Time" Header="Time"/>
    </GridViewColumnCollection>

    <DataTemplate x:Key="header1">
    <StackPanel Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumnHeader}}}">
    <GridViewColumnHeader Content="Name" />
    <GridViewColumnHeader Content="Level"/>
    </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="header2">
    <StackPanel Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type GridViewColumnHeader}}}">
    <GridViewHeaderRowPresenter Columns="{StaticResource c1}"/>
    <GridViewColumnHeader Content="Name"/>
    </StackPanel>
    </DataTemplate>
    <DataTemplate x:Key="cell1">
    <StackPanel>
    <TextBlock Text="{Binding XPath=@Name}"/>
    <TextBlock Text="{Binding XPath=@Level}"/>
    </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="cell2">
    <StackPanel>
    <GridViewRowPresenter Columns="{StaticResource c1}"/>
    <TextBlock Text="{Binding XPath=@Artist}"/>
    </StackPanel>
    </DataTemplate>


    </StackPanel.Resources>
    <ListView ItemsSource="{Binding Source={StaticResource MyData}, XPath=Song}">
    <ListView.View>
    <GridView>
    <GridViewColumn HeaderTemplate="{StaticResource header1}" CellTemplate="{StaticResource cell1}"/>
    <GridViewColumn HeaderTemplate="{StaticResource header2}" CellTemplate="{StaticResource cell2}"/>
    </GridView>
    </ListView.View>
    </ListView>

    </StackPanel>


  • Dee_dotnet_79

    Instead of

    RelativeSource={RelativeSource AncestorType={x:Type GridViewColumnHeader}}

    Please try

    RelativeSource=/ContentControl


  • Panaxea

    The lines with the RelativeSource are not compiling:

    The value '{RelativeSource AncestorType={x:Type GridViewColumnHeader}}' is not a valid MarkupExtension expression. Could not resolve 'RelativeSource' in namespace 'http://schemas.microsoft.com/winfx/avalon/2005'. 'RelativeSource' must be a subclass of MarkupExtension.

    I'm using the January CTP of WPF.


  • nic667

    try this

    <StackPanel Name="sp1" Height="800" Width="800">

    <StackPanel.Resources>

    <DataTemplate x:Key="ct1">

    <Grid >

    <ColumnDefinition/>

    <ColumnDefinition/>

    <RowDefinition/>

    <RowDefinition/>

    <TextBlock Grid.Column="0" Grid.Row="0" >

    <TextBlock.Text>

    <Binding XPath="@orderid"/>

    </TextBlock.Text>

    </TextBlock>

    <TextBlock Grid.Column="1" Grid.Row="0" >

    <TextBlock.Text>

    <Binding XPath="@orderdate"/>

    </TextBlock.Text>

    </TextBlock>

    <TextBlock Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2">

    <TextBlock.Text>

    <Binding XPath="@address"/>

    </TextBlock.Text>

    </TextBlock>

    </Grid>

    </DataTemplate>

    <XmlDataProvider x:Key="qq" XPath="/customers">

    <customers xmlns="">

    <c customerid="ALFKI" contactname="Maria Anders">

    <o orderid="10643" orderdate="1997-08-25T00:00:00" address="Obere Str. 57; Berlin" />

    <o orderid="10692" orderdate="1997-10-03T00:00:00" address="Obere Str. 57; Berlin" />

    </c>

    <c customerid="ANATR" contactname="Ana Trujillo">

    <o orderid="10308" orderdate="1996-09-18T00:00:00" address="Avda. de la Constituci¢n 2222; M xico D.F." />

    <o orderid="10625" orderdate="1997-08-08T00:00:00" address="Avda. de la Constituci¢n 2222; M xico D.F." />

    </c>

    <c customerid="ANTON" contactname="Antonio Moreno">

    <o orderid="10365" orderdate="1996-11-27T00:00:00" address="Mataderos 2312; M xico D.F." />

    </c>

    <c customerid="AROUT" contactname="Thomas Hardy">

    <o orderid="10355" orderdate="1996-11-15T00:00:00" address="Brook Farm Stratford St. Mary; Colchester" />

    <o orderid="10383" orderdate="1996-12-16T00:00:00" address="Brook Farm Stratford St. Mary; Colchester" />

    <o orderid="10453" orderdate="1997-02-21T00:00:00" address="Brook Farm Stratford St. Mary; Colchester" />

    <o orderid="10558" orderdate="1997-06-04T00:00:00" address="Brook Farm Stratford St. Mary; Colchester" />

    </c>

    </customers>

    </XmlDataProvider>

    </StackPanel.Resources>

    <ListView Name="list1" IsSynchronizedWithCurrentItem="true" ItemsSource="{Binding Source={StaticResource qq}, XPath=c}">

    <ListView.View>

    <GridView >

    <GridView.Columns>

    <GridViewColumn Header="customerid" DisplayMemberPath="@customerid" />

    <GridViewColumn Header="contactname" DisplayMemberPath="@contactname"/>

    </GridView.Columns>

    </GridView>

    </ListView.View>

    </ListView>

    <ListView ItemTemplate="{StaticResource ct1}" Name="list2" IsSynchronizedWithCurrentItem="true" DataContext="{Binding ElementName=list1, Path=SelectedItem}" ItemsSource="{Binding XPath=o}">

    </ListView>

    </StackPanel>



  • Getting ListView to do things other than text