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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.