Why are Groups in WPF ListView empty?

Viewed 454

I have a class in my application with three properties GroupName, ItemName, and Data. I am have a ListView to group a collection of these classes by GroupName and display their ItemName property in a text box. The problem is that when I run the code, the groups appear correctly but none of them display any members.

Here is the xaml code:

<ListView x:Name="MyList">
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Expander IsExpanded="True">
                                    <Expander.Header>
                                        <TextBlock FontWeight="Bold" Text="{Binding Name}"/>
                                    </Expander.Header>
                                </Expander>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
    <ListView.ItemTemplate>
        <DataTemplate DataType="{x:Type testProgram:MyClass}">
            <TextBlock Text="{Binding ItemName}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And here is the code behind:

public partial class MyListView
{
    public MyListView(ObservableCollection<MyClass> items)
    {
        InitializeComponent();
        Items = items;

        var v = CollectionViewSource.GetDefaultView(Items);
        v.GroupDescriptions.Add(new PropertyGroupDescription("GroupName"));
        MyList.ItemsSource = v;
    }

    public ObservableCollection<MyClass> Items { get; set; }
}

When I remove the <ListView.GroupStyle>... and set MyList.ItemsSource = Items; then everything shows up properly. I suspect that the issue is between ItemsSource = v, DataType = "{x:Type testProgram:MyClass}", and {Binding ItemName}, but I don't know what is breaking or how to fix it.

1 Answers
Related