【发布时间】:2012-08-14 21:19:59
【问题描述】:
我有一个由分组数据填充的列表框,下面有一个标题和子项 - 我想自定义控件,使每个组标题都是一个扩展器(最初关闭),展开它会显示它下面的该组的项目
我还是 WPF 的新手,我该怎么做呢?我是否需要为 GroupStyle 的 HeaderTemplate(这里是否包含 ItemsPresenter?)和 ItemTemplate 实现数据模板,或者我是否需要在这两个数据模板之外创建一个全新的 ControlTemplate?
另外,对标题使用扩展器会禁用 ListBox 的项目虚拟化功能吗?如果是这样,我该怎么做才能确保不会发生这种情况?如果我将 VirtualizingStackPanel 放在 Expander 内容中,会减轻这种情况还是没有必要?
我的分组非常简单,只是一个字段上的平面集合:
ICollectionView collegeView = new ListCollectionView(playerDatabase);
collegeView.GroupDescriptions.Add(new PropertyGroupDescription("CollegeName"));
collegeView.SortDescriptions.Add(new SortDescription("CollegeName", ListSortDirection.Ascending));
collegeView.SortDescriptions.Add(new SortDescription("FirstName", ListSortDirection.Ascending));
lstCollege.ItemsSource = collegeView;
更新 这是我迄今为止尝试过的:
<ListBox x:Name="lstCollege" IsSynchronizedWithCurrentItem="True">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Border Background="Blue" CornerRadius="5">
<Expander Header="{Binding Path=Name}" FontWeight="Bold" Foreground="White" Padding="3">
<ItemsPresenter />
</Expander>
</Border>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
不幸的是,这呈现的是每个组的扩展器,但是项目并未呈现在其中 - 它们呈现在组项目之外并且扩展器是空的
我认为发生这种情况的原因是因为 GroupItem 与 ListItem (或其他任何名称)分开呈现,因此 ListViewItem 不包含在每个 GroupItem 内......我认为 ListBox 呈现它的方式是 GroupItem, ListItem、ListItem、ListItem、GroupItem、ListItem 等
如何更改控件以使每个 GroupItem 也可以包含一组 ListItem?
【问题讨论】:
-
发布一些代码会有所帮助 - 你到底是如何进行分组的?你的
ItemsSource是平的吗?标题和项目都在同一个列表中吗?还是标题复杂类型具有定义子级的属性?我不相信在DataTemplate中使用Expander会自动阻止虚拟化。 -
发布了示例代码。我的问题的重点更多是为了获得我所描述的行为,我需要实现的最低限度是什么——我可以只定义 DataTemplates 还是需要定义整个 ControlTemplate?如果我需要创建一个 ControlTemplate,我需要做的最低限度是什么才能保留 ListBox 功能?
-
好的,您使用 ICollectionView 的事实说明了您是如何进行分组的。请参阅下面的答案。