使用分组绑定WPF中的列表框 [英] Bind listbox in WPF with grouping
问题描述
我有一个ViewModels集合,想要将一个ListBox绑定到它们.经过调查,我发现了这.
I've got a collection of ViewModels and want to bind a ListBox to them. Doing some investigation I found this.
所以我的ViewModel看起来像这样(伪代码)
So my ViewModel look like this (Pseudo Code)
interface IItemViewModel
{
string DisplayName { get; }
}
class AViewModel : IItemViewModel
{
string DisplayName { return "foo"; }
}
class BViewModel : IItemViewModel
{
string DisplayName { return "foo"; }
}
class ParentViewModel
{
IEnumerable<IItemViewModel> Items
{
get
{
return new IItemViewModel[] {
new AItemViewModel(),
new BItemViewModel()
}
}
}
}
class GroupViewModel
{
static readonly GroupViewModel GroupA = new GroupViewModel(0);
static readonly GroupViewModel GroupB = new GroupViewModel(1);
int GroupIndex;
GroupViewModel(int groupIndex)
{
this.GroupIndex = groupIndex;
}
string DisplayName
{
get { return "This is group " + GroupIndex.ToString(); }
}
}
class ItemGroupTypeConverter : IValueConverter
{
object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is AItemViewModel)
return GroupViewModel.GroupA;
else
return GroupViewModel.GroupB;
}
}
这个XAML
<UserControl.Resources>
<vm:ItemsGroupTypeConverter x:Key="ItemsGroupTypeConverter "/>
<CollectionViewSource x:Key="GroupedItems" Source="{Binding Items}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription Converter="{StaticResource ItemsGroupTypeConverter }"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<ListBox ItemsSource="{Binding Source={StaticResource GroupedItems}}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}" FontWeight="bold" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DisplayName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这可以以某种方式起作用,例如HeaderTemplate的绑定不起作用的事实.无论如何,我都希望省略TypeConverter和CollectionViewSource.没有办法使用ViewModel的属性进行分组吗?
This works somehow, exept of the fact that the binding of the HeaderTemplate does not work. Anyhow I'd prefer omitting the TypeConverter and the CollectionViewSource. Isn't there a way to use a property of the ViewModel for Grouping?
我知道,在此示例场景中,将GroupViewModel替换为可以正常工作的字符串很容易,但这不是一个选择.那么如何将HeaderTemplate绑定到GroupViewModel?
I know that in this sample scenario it would be easy to replace the GroupViewModel with a string an have it working, but that's not an option. So how can I bind the HeaderTemplate to the GroupViewModel?
推荐答案
好,我终于自己解决了.
Ok, I finally solved it myself.
首先不需要TypeConverter,因为PropertyGroupDescription可以绑定到PropertyName上.因此,我向IItemViewModel添加了一个属性"Group",并按如下所示修改了XAML:
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>
此外,HeaderTemplate的DataContext是CollectionViewGroupInternal,绑定必须看起来像这样:
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>
此处CollectionViewGroupInternal.Name解析实际的GroupViewModel.
Here CollectionViewGroupInternal.Name resolves the actual GroupViewModel.
这篇关于使用分组绑定WPF中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!