Bind listbox in WPF with grouping

北慕城南 提交于 2019-12-03 22:04:27

Ok, I finally solved it myself.

First of all the TypeConverter is unnecessary, because the PropertyGroupDescription can be bound on a PropertyName. So I added a Property 'Group' to IItemViewModel and modified XAML as follows:

<UserControl.Resources>
 <CollectionViewSource x:Key="GroupedItems" Source="{Binding Items}">
  <CollectionViewSource.GroupDescriptions>
   <PropertyGroupDescription PropertyName="Group"/>
  </CollectionViewSource.GroupDescriptions>
 </CollectionViewSource>
</UserControl.Resources>

Furthermore the HeaderTemplate's DataContext is a CollectionViewGroupInternal and the binding has to look like this:

<DataTemplate>
 <TextBlock Text="{Binding Name.DisplayName}" FontWeight="bold" />
</DataTemplate>

Here CollectionViewGroupInternal.Name resolves the actual GroupViewModel.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!