【发布时间】:2018-01-26 06:27:43
【问题描述】:
我可以找到在 WPF 中将Expander 实现为ListView 的组的所有示例,方法是在GroupStyle.ContainerStyle 属性中为GroupItem 定义一个新的ControlTemplate。
可以在here 找到一个这样的例子,它可以按预期工作。
但是它确实有一个缺陷,如果您添加额外的GroupDescriptions(用于多级分组),则第二级和后续级别没有缩进,就像仅覆盖标题模板而不是容器时那样风格。
我碰巧注意到GroupItem 代码确实似乎特别支持在其模板中定位Expander 控件,这(如果那是递归搜索)表明可能可以替换该组上面的样式用一个更简单的样式,如下所示:
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate DataType="GroupItem">
<Expander IsExpanded="True">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
FontWeight="Bold" Foreground="Gray"
FontSize="22" VerticalAlignment="Bottom" />
<TextBlock Text="{Binding ItemCount}" FontSize="22"
Foreground="Green" FontWeight="Bold"
FontStyle="Italic" Margin="10,0,0,0"
VerticalAlignment="Bottom" />
<TextBlock Text=" item(s)" FontSize="22"
Foreground="Silver" FontStyle="Italic"
VerticalAlignment="Bottom" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
即。相同的核心Expander,但表示为HeaderTemplate,而不是ContainerStyle。
这几乎有效——在视觉上它看起来是正确的,甚至有多个级别的预期缩进。但是切换Expander实际上并没有显示或隐藏组中的项目,因此很明显视觉层次结构是错误的。
我有几个问题:
- 有没有办法让它像这样工作?
- 如果不是,
GroupItem对Expander检测实际上做了什么,如果不是为了这个? - 坚持原来的
ContainerStyle,有没有办法保持自然压痕?
【问题讨论】:
标签: c# .net wpf listview expander