Question

I have a basic program in which I'm trying to test a possible layout of ListView, so that I can use it in my main project. I'm having a problem displaying the headers for the groups and I can't figure it out. It's obviously a binding issue, but after much searching and reading through many ListView grouping/header related postings, I haven't been able to solve it. I prefer to get it all done within XAML, since my ViewModels will have no knowledge of the Views. I can display regular text, but not the binding when dealing with the group headers. Here's my code:

View:

<Window.Resources>
    <CollectionViewSource Source="{Binding Collection}" x:Key="GroupedItems">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="RepNum" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

<ListView HorizontalAlignment="Left" Height="300" Margin="10,10,0,0" 
          VerticalAlignment="Top" Width="497"
          ItemsSource="{Binding Source={StaticResource GroupedItems}}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="File Name" 
                            DisplayMemberBinding="{Binding FileName}"/>
            <GridViewColumn Header="Date Scanned" 
                            DisplayMemberBinding="{Binding DateScanned}"/>
            <GridViewColumn Header="Time Scanned" 
                            DisplayMemberBinding="{Binding TimeScanned}"/>
        </GridView>
    </ListView.View>

    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding RepNum}" Margin="0,0,10,0"/>
                        <TextBlock Text="Text1" Margin="0,0,10,0"/>
                        <TextBlock Text="Text2"/>
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>
</ListView>

ViewModel (very dirty, quick-style for testing purposes):

public class MainWindowViewModel
{
    public ObservableCollection<Record> Collection { get; set; }

    public MainWindowViewModel()
    {
        Collection = new ObservableCollection<Record>();

        Collection.Add(
            new Record()
            {
                RepNum = "12345",
                FileName = "12345.pdf",
                DateScanned = "05/01/2014",
                TimeScanned = "1802"
            });

        Collection.Add(
            new Record()
            {
                RepNum = "12345",
                FileName = "12345C1.pdf",
                DateScanned = "05/03/2014",
                TimeScanned = "1401"
            });

        Collection.Add(
            new Record()
            {
                RepNum = "12345",
                FileName = "12345S1.pdf",
                DateScanned = "05/03/2014",
                TimeScanned = "1408"
            });

        Collection.Add(
            new Record()
            {
                RepNum = "643334",
                FileName = "643334.pdf",
                DateScanned = "02/01/2013",
                TimeScanned = "1305"
            });

        Collection.Add(
            new Record()
            {
                RepNum = "643334",
                FileName = "643334S1.pdf",
                DateScanned = "02/06/2013",
                TimeScanned = "0805"
            });
    }
}

You can guess what Record class looks like by looking at how I create instances of it, so there's nothing special there. I don't have a problem viewing RepNum on any binding, except the ListView group header. I'm obviously either missing a step or don't have a proper binding syntax for its case. However, I can't figure it out.

Here's what it looks like:

enter image description here

As you can see, plain text "Text1" and "Text2" are displayed, but not the RepNum property.

I tried:

Text="{Binding Source={StaticResource GroupedItems}, Path=RepNum}"

...but that displays RepNum of the first group for every header:

enter image description here

Was it helpful?

Solution

Well, I think I found a proper answer:

<StackPanel Orientation="Horizontal" DataContext="{Binding Items}">
    <TextBlock Text="{Binding Path=RepNum}" Margin="0,0,10,0"/>
    <TextBlock Text="Test1" Margin="0,0,10,0"/>
    <TextBlock Text="Test2"/>
</StackPanel>

I guess I had to set the DataContext of my StackPanel to Items and then I could bind to RepNum.

If anyone has a better way, let me know. This one works, though.

Finally came across an answer that helped me: https://stackoverflow.com/a/19232635/2006048

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top