2

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.

0

1 Answer 1

1

Your ControlTemplate is missing an ItemsPresenter. WPF uses an ItemsPresenter in the GroupItem template to mark the location where the actual items will be placed when the template is expanded. Since you have no presenter, the detail items are not displayed.

Try changing your template to this:

<Expander IsExpanded="True">
    <Expander.Header>
        <TextBlock FontWeight="Bold" Text="{Binding Name}"/>
    </Expander.Header>
    <Expander.Content>
        <ItemsPresenter />
    </Expander.Content>
</Expander>
Sign up to request clarification or add additional context in comments.

1 Comment

that was it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.